logo

[JavaScript] Check for Alpha Numeric Characters

Get cut-n-paste solving scripts for your website. Scripts are for all languages.

[JavaScript] Check for Alpha Numeric Characters

Postby timdav83 » Sat Dec 19, 2009 4:36 am

Simple function to check for alpha numeric input.

Small Explanation:

for is our basic loop. It basically says store the value of 0 in x. The x++ increments the value of x by 1. str.length determines how many characters are in the variable str. So for tim for example, this contains 3 characters, so the loop re-writes to x=0; x <= 3-1; x++. I have found a hidden character in the variable, which is why I added str.length-1. So it will actually be 2 rather then 3.

str.charAt method will take the current value of x and check what character is filling that position within the string you input. If x is 1 and it's looping through my name tim, the method will determine that i is filling the position of 1.

valid.indexOf takes the letter that str.charAt found and compares it to the valid string / variable. The valid variable contains the numbers 1-0, a-z and A-Z. So, for tim, the letter t exists and takes up position 51 in the valid string, i takes position 40, and m is position 44. ( I'm counting the entire valid variable starting from left to right beginning at the number 0, which is 1 ) If we have % in the input for example, it will return -1 because the method will not be able to locate it in the valid string.

i=i+1 I have this in there to determine how many characters valid.indexOf( str.charAt( x ) cannot find in the valid string. It increments each time it can't find something.

return (i == 0) ? true : false; states that if a number stored in i is anything other then 0, return false. So if the string contains invalid characters, i will not equal 0 because it would have incremented by 1.

I hope this is an adequate explanation.

Code: Select all
function alpha_numeric(str)
{

   var valid = "1234567890ABCDEFGHIJKLMNOPQRSTUVabcdefghijklmnopqrstuvwxyz";
   var i=0;
   
   for (x = 0; x <= str.length-1; x++)
   {
      if( valid.indexOf( str.charAt( x ) ) == -1 )
      {
         var i = i+1;
      }       
   }   
   
   return (i == 0) ? true : false;
}


Simple Usage:

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Alpha Numeric Javascript</title>

<script type="text/javascript">

function alpha_numeric(str)
{

   var valid = "1234567890ABCDEFGHIJKLMNOPQRSTUVabcdefghijklmnopqrstuvwxyz";
   var i=0;
   
   for (x = 0; x <= str.length-1; x++)
   {
      if( valid.indexOf( str.charAt( x ) ) == -1 )
      {
         var i = i+1;
      }       
   }   
   
   return (i == 0) ? true : false;
}

function check()
{
   var str = document.getElementById('textfield').value;
   
   if( alpha_numeric(str) )
   {
      alert("valid");
   } else {
      alert("invalid");
   }
   
}

</script>
</head>

<body>
<form>
  <p>Enter Text:
    <input type="text" name="textfield" id="textfield" />
    <br />
    <br />
    <input type="button" name="button" id="button" value="Submit" onClick="check()"/>
  </p>
</form>
</body>
</html>

User avatar
timdav83
 
Posts: 6539
Joined: Tue Feb 11, 2003 2:31 am
Location: Ohio, USA

Re: [JavaScript] Check for Alpha Numeric Characters

Postby Heaven » Sat Dec 19, 2009 8:00 am

Thank you Tim for the code, i'll have a test :shades:

Samy
User avatar
Heaven
 
Posts: 1611
Joined: Wed Oct 06, 2004 4:12 am
Location: Iran


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest