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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation code

Status
Not open for further replies.

spicymango

Programmer
May 25, 2008
119
CA
I need to put a validation code that allow only Letter, Numbers abd spaces. I have the following code that allows only Letter and numbers. but not spaces, how can I make it so it allow spaces as well?

Thanks

Code:
<script type="text/javascript">
function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
                         alert("Your Alpha Numeric Test Failed");
			 return false;
		  }
 		}
 alert("Your Alpha Numeric Test Passed");
 return true;
}
</script>


 
A quick look at shows me that the ASCII code for a space is decimal 32, so just add that to your list of acceptable alternatives.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Regular Expressions may be another option for you, your validation needs are simple enough it would seem in this case for the regex not to be too complex.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
[tt]function alphanumeric(alphane)
{
return [highlight]![/highlight]/[^[highlight] [/highlight]0-9A-Za-z]/.test(alphane);
}[/tt]
 
One question in you regex, How does it know to include space also as true?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top