PDA

View Full Version : Trouble with form VERIFICATION


BigZAJ
12-29-2005, 06:06 AM
I have this code to verify my data fields:

if (document.survey.user.value=='')
{
error = error + ' * User Name';
}
if (document.survey.wkordernumber.value=='')
{
error = error + ' * Work Order #';
}

for(i = 0; i < document.survey.wkordernumber.length; i++)
{
if((document.survey.wkordernumber.charCodeAt(i) >57)||(document.survey.wkordernumber.charCodeAt(i) < 48)
{
error = error + '* Invalid Work Order #';
}
}

if I take out the for loop it works fine, with the for loop none of the checks work. is the loop correct? I am trying to check if the value at the wkordernumer contains any characters that arent numbers. THANKS

JerSchneid
12-30-2005, 10:36 AM
A couple suggestions:

First, be careful about doing javascript form validation. It's really easy to turn JavaScript off which leaves your site open to a really easy hack. Instead do your verification on the server side using ASP.Net.

Second, I suggest using FireFox to debug the JavaScript on your site. I would bet that your for loop is causing a crash which has the effect of skipping the rest of the function (and hence you don't see whatever is at the end of your function) FireFox has a really cool 'JavaScript console' that will tell you exactly what is crashing and where.

Finally, check out the ASP.Net Validation Controls (http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp). These will do everything you are trying to do, while validating on the server side AND client side (the best of both worlds). If I were you I would take an hour to learn how to use them. It will definitely pay off in the future.

Sorry I didn't give you a quick fix to your JavaScript, but hopefully my advice helps you build more powerful forms in the future!

Jeremy

p.s., by the way I think you want to change your if statement to this:

if((document.survey.wkordernumber.value.charCodeAt (i) >57)||(document.survey.wkordernumber.value.charCode At(i) < 48)