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!

Missing variable in my javascript???

Status
Not open for further replies.

Nina2001

Programmer
Dec 28, 2001
58
US
Hi. Can someone take a look at this? I'm trying to validate this form field during the html form submission. I want it to make sure the field contains alpha numeric characters with a length of 9. Right now it is only letting a user submit 9 numbers. Any help is greatly appreciated.

if (theForm.ID.value.length != 9)
{
alert("Please enter only 9 characters, with no dashes or spaces, in the \"ID\" field.");
theForm.ID.focus();
return (false);
}
var checkOK = "0123456789";
var checkStr = theForm.ID.value;
var allValid = true;
var decPoints = 0;
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;
}
allNum += ch;
}
if (!allValid)
{
alert(&quot;Please enter only 9 characters, with no dashes or spaces, in the \&quot;ID\&quot; field.&quot;);
theForm.ID.focus();
return (false);
}
return (true);
}
 
With as many questions as I ask here, I probably shouldn't try to answer one but wouldn't it be easier to say

if (len(string) > 9) {
...action...
}

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
if (theForm.ID.value.length != 9)
{
alert(&quot;Please enter only 9 characters, with no dashes or spaces, in the \&quot;ID\&quot; field.&quot;);
theForm.ID.focus();
return (false);
}
var ptrn = /^\w{9}$/
var checkStr = theForm.ID.value;

if (!ptrn.test(checkStr))
{
alert(&quot;Please enter only 9 characters, with no dashes or spaces, in the \&quot;ID\&quot; field.&quot;);
theForm.ID.focus();
return (false);
}
return (true);
}




Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top