Click to See Complete Forum and Search --> : Checking for alphabet and numeric chars in a field


Ann
07-09-2003, 08:38 AM
Hi Guyz,
I am trying to write javascript code for making sure a field in my form has first 2 characters as alphabets(US) and the rest 8 are numerics.total has to be 10.if any of these r not satisfied,i shud throw up a alert box.i am checking for numerics now but dont know how to combine checking for alphabets n numerics in the same field....
here is my code for checking for numerics...please help me out...sid is my field name...
// only allow numbers to be entered
var checkOK = "0123456789";
var checkStr = document.forms[0].sid.value;
var allValid = true;
var allNum = "";
{
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;


if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only 8 numeric characters in the \"sid\" field.");
return (false);
}

Khalid Ali
07-09-2003, 08:44 AM
I think your best bet will be RegExp, soon some php/perl guru will guide you on this....

Ann
07-09-2003, 08:52 AM
Does that mean i cant do it in Javascript :confused:

Khalid Ali
07-09-2003, 08:56 AM
yes you will be able to do that in JavaScript,its only that trying to do it the way you are doing could be slower,regular expression is cleaner and efficient way of parsing strings...

Webskater
07-09-2003, 09:06 AM
You could do it easily enough as data is entered.

This code tells you what key was depressed.
var key = event.keyCode;

This code stops people from entering anything other than 0 to 9

//make sure keys are numeric
if (key<48 || key>57) event.returnValue=false;

This code stops people adding anything other than a-Z

var key = event.keyCode;
//make sure keys are alphabet
if (key<97 || key>122) event.returnValue=false;

So all you have to do is write a function that is called onkeypress, check the length of the field and apply whichever bit is required to force people to only put a-z or 0-9 in the appropriate places. If they try to type anything else - nothing happens - although you would presumably put an alert box up to tell them what they have done wrong.

Fang
07-09-2003, 09:24 AM
This function returns TRUE if it validates, FALSE if it doesn't.

function Validate(FieldValue) {
var objRegExp=/^[a-zA-Z]{2}[1-9]\d{8}$/;
return objRegExp.test(FieldValue);
}

Try one of these (http://www.google.com/search?sourceid=navclient&ie=UTF-8&oe=UTF-8&q=regular+expressions+javascript) for more on regular expressions.

pyro
07-09-2003, 10:25 AM
Originally posted by Fang
var objRegExp=/^[a-zA-Z]{2}[1-9]\d{8}$/;
Get rid of the [1-9]...

Ann
07-09-2003, 10:50 AM
I tried the onkeypress() function but i have to make sure my first 2 characters are U and S only and nothin else..followed by 8 digits of numeric only.i am unable to do that using the onkeypress code u suggested.IS there any other way or code i do it.I am new to javascript so dont know how to do complicated stuff yet.Please help.
Thanks for all the suggestions..

Ann
07-09-2003, 10:52 AM
I also shud throw an alert box if the user doesn't enter as required...

Ann
07-09-2003, 11:18 AM
This is another thing i tried..
But this does'nt seem to work either...

function Validate(sid)
{
var objRegExp=/^[US]{2}\d{8}$/;
return objRegExp.test(sid);
if (sid = false)
{
alert("Please make sure SID number starts with 'US' followed by 8 numbers");
}
}

Jeff Mott
07-09-2003, 11:29 AM
/^[US]{2}\d{8}$/I think everyone misunderstood and thought you wanted any two alphabetic characters. In any case, /^US\d{8}$/function Validate(sid)
{
var objRegExp=/^[US]{2}\d{8}$/;
return objRegExp.test(sid);
if (sid = false)
{
alert("Please make sure SID number starts with 'US' followed by 8 numbers");
}
}Because of your placement of the return statement, no other statements in that function will be evaluated. You need to close the function brace after the return then pass your variable to be tested to Validate(). Also note the inside of your if statement. One equal sign (=) means assignment and two equal signs (==) means test equality.function validate(sid)
{
return /^US\d{8}$/.test(sid);
}

if (!validate(document.your_text_box.value))
alert("Please make sure SID number starts with 'US' followed by 8 numbers");

Ann
07-09-2003, 12:01 PM
I am really sorry bugging you all like this but it still does'nt work..i used the following code and in my notes JS debugger it shows no error but when i actually open my browser and try to run the form it shows error on page...and no alert box gets thrown up when i enter letters in the numeric fields..
again,really sorry for this but i need to get this done and cant quite figure what i am doing wrong...

function validate(sid)
{
return /^US\d{8}$/.test(sid);
}

if (!validate(document.sid.value))
alert("Please make sure SID number starts with 'US' followed by 8 numbers");

Ann
07-09-2003, 02:37 PM
Thanks a lot Jeff and everyone...it works..
i was making some silly mistakes in the syntax of the code.
:D