Click to See Complete Forum and Search --> : newbie question


yardleybates
06-02-2003, 02:05 PM
Is there an existing client side string function to check for alpha numeric?
I need to add some data verification on our website, and would prefer not to reinvent the wheel.

Thanks

scriptkid
06-02-2003, 02:10 PM
you could use a regex

function isAlphaNumeric sString )
{
var reg = new RegExp(/^\w$/);
if (reg.test(sString)){ return false; }
return true;
}


might need to check the patern to make sure its right but i think its right :(

Jona
06-02-2003, 02:14 PM
I think you're talking about a Regular Expression, are you not? Something to check whether the character(s) in the textfield are numbers or letters only? That should be something like this:


<script type="text/javascript">
<!--
function check(f){
var re = /[a-z0-9]/gi;
if(f.t1.value.test(re)){return true;}
return false;
}
//-->
</script></head>
<body>
<form action="" onsubmit="check(this);"><div>
<input type="text" name="t1"><br>
<input type="submit"></div>
</form></body></html>


Jona

scriptkid
06-02-2003, 02:17 PM
in Jona's example you would have to lowercase the string you are testing first...or make a small adjustment :)

function check(f){
var re = /[a-zA-Z0-9]/gi;
if(f.t1.value.test(re)){return true;}
return false;
}
although im not sure what the "gi" is for if u could explain plz Jona id be most appreciative

Jona
06-02-2003, 02:20 PM
Actually, the regex is not case sensetive, because of the "gi." The g is for, "global," which means that it will test all multiple instances of the RegExp, the "i" is for case-insensetive so that it will match A, a, B, b, C, c, and so on.

Jona

yardleybates
06-02-2003, 02:33 PM
thanks all that is exactly what I needed :cool:

Jona
06-02-2003, 02:34 PM
You're welcome.

Jona

yardleybates
06-02-2003, 02:40 PM
one more question, if I may ...
I have not used the .test method and I don't find reference to it in MSDN java reference. Can you briefly explain the syntax / functionality of this?

AdamGundry
06-02-2003, 02:42 PM
Check out the Netscape reference:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/regexp.html#1194128

Adam

scriptkid
06-02-2003, 02:42 PM
object.test(string test);

It takes the regular expression pattern (/[a-z0-9]/gi)
and compares it against the string you provide the method

if the string you provided matches the pattern then it returns true else it returns false

thanks btw Jona for the gi thing ;) didnt know about that

Jona
06-02-2003, 02:44 PM
You can also do it your way, scriptkid:


function isAlphaNumeric(sString){
var reg = new RegExp(/[a-z0-9]/, "gi");
if(reg.test(sString)){return false;}
return true;
}


Jona

yardleybates
06-02-2003, 02:47 PM
thanks again all =)