Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check every character in all text boxes onSubmit 2

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
KW
Hi there,

I have one question, how can we onSubmit form action check all fields values if they contain a specific charatcer (in regardless what is the field name or how many fields, they might be only a text box).

Lets say i want to raise an alert if on of the text boxes have a letter '~' or '`' or '|' etc..

I managed to make a function to check while the user is typing, but they can copy any text from a notepad then paste it in the text box! Because of this I want to make another check onSubmit.

Thanks in advance :)
 
Loop through form.elements, check only inputs of type text, analyze values, exit if any of blacklisted characters is present:
Code:
<form ... onsubmit="return test( this )">

<script language="javascript">
function test( oFrm )
{	for( i=0; i < oFrm.elements.length; i++ )
	{	oElem = oFrm.elements[i];
		if (oElem.tagName.toLowerCase()=="input" && oElem.type.toLowerCase()=="text")
			if ( oElem.value.match( /\||\^|~/ ))
			{	alert( "No-no...");
				oElem.focus();
				return false;
			}
	}
	
	return true;
}
</script>
 
Thanx vongrunt for your great help, you are genius! now I have a questoin:

I don't understand the match property (Is there's any clear tutorial about match property)! How can we add more characters?
 
This is regular expression pattern. There are many related tutorials available on web, plus some help files to download (like js55.chm from MSDN). Here are some guidelines about stuff used:

- leading and trailing slashes (/) are part of javascript syntax
- pipe (|) means "OR"
- backslash is escape character, useful to define characters that are otherwise part of regexp syntax. \| isn't OR; it is interpreted as literal "|"
- same applies for \^. Caret (^) is regexp anchor, \^ represents "^"
- characters that aren't part of regexp syntax don't require escaping (in example above ~)

So basically this turns into: search for "|" or "^" or "~". Perhaps better in your case is the following pattern:

/[\|\^~]/

It groups symbols with square braces ([]) and doesn't require OR stuff.

If this looks cryptic, you can use vanilla method without regexp:

Code:
		...
		if ( matchAnyCharacter( oElem.value, "|^~" ) )
		{	alert( "No-no...");
			...

function matchAnyCharacter( sStr, sChars )
{	for( var bRet=false, i=0; !bRet && i<sChars.length; i++)
		bRet = sStr.indexOf( sChars.substr(i, 1) )!= -1;
		
	return bRet;
}
 
vongrunt as I told you before, thank you very much for every single minute you spent in helping me.

Thanks again the JS genius.
 
aha, so u have become a RegEx maniac too... ;)

Known is handfull, Unknown is worldfull
 
vongrunt I serached for js55.chm but I couldn't find! can you provide me with a link
 
My dear,

Can I put a tag atribute in all my text input so I can validate the input based on the tag. i.e. input that expected to have number i'll make it <input tag='NumbersOnly' name='Age'> and another input which expected to have only string i'll make it <input type='StringOnly' name='FName'> and then in my code:

<script language="javascript">
function test( oFrm )
{ for( i=0; i < oFrm.elements.length; i++ )
{ oElem = oFrm.elements;
if (oElem.tagName.toLowerCase()=="input" && oElem.type.toLowerCase()=="text" && oElem.tag=="NumbersOnly")
if ( oElem.value.match( /\||\^|~/ ))
{ alert( "No-no...");
oElem.focus();
return false;
}
}

return true;
}
</script>

Its not working after adding:

&& oElem.tag=="NumbersOnly"

??!!
 
This would probably work in IE (if you changed 'type' to 'tag' in "<input type='StringOnly' name='FName'> "), but to make it cross-browser compatible, you might stick these identifiers in the ID attribute. Not the best use of that attribute, but it's available.

--Dave
 
But its not working, where did I go wrong in my code?!

I'm testing it via IE6.
 
Since 'tag' is not standard, you might have to test for it's existance first:

if(oElem.tagName.toLowerCase()=="input" && oElem.type.toLowerCase()=="text" &&
(oElem.tag && oElem.tag=="NumbersOnly")
)


If that doesn't work, I would try a different word than 'tag' for this attribute.

Let us know.

--Dave
 
Sorry :) I wrote the tag name not same as the tag name in the script ;) Its working perfect with && oElem.tag=="NumbersOnly" only

Thanks alot everybody :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top