Trouble with form VERIFICATION

Discussion in 'HTML / PHP / JavaScript / CSS' started by BigZAJ, Dec 29, 2005.

  1. 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
     
  2. 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. 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.charCodeAt(i) < 48)
     

Share This Page