numeric validation ! pls help[urgent]

javascript United States
  • 18 years ago

    hie..i'm a javascript newbie..can somebody help me with these codes??? been stuck with it for sometime????


    is there any other way i can validate numeric numbers before submitting the form???




    here are my codes....


    <HTML>
    <HEAD>



    <SCRIPT>
    function Validator(theForm)
    {


     if (frmCusInfo.txtFName.value == "")
       {
       alert("Please enter your name for the \"First Name\" field.");
       frmCusInfo.txtFName.focus();
       return (false);
       }    


     if (frmCusInfo.txtLName.value == "")
       {  
       alert("Please enter your name for the \"Last Name\" field.");
       frmCusInfo.txtLName.focus();
       return (false);
       }    
       
     if (frmCusInfo.txtAge.value == "")
       {  
       alert("Please enter your age for the \"Age\" field.");
       frmCusInfo.txtAge.focus();
       return (false);
       }      
     
     
    // NEEDING HELP HERE!!  
    //if (IsNaN(frmCusInfo.txtAge.value))
    //  if (IsNaN(100))
    //  {  
    //   alert("Please enter a numeric number for the \"Age\" field.");
    //   frmCusInfo.txtAge.focus();
    //  return (false);
    // }    




    </SCRIPT>
    </HEAD>
    <BODY>  
    <FORM name=frmCusInfo  onsubmit="return Validator(this)" method=post>
    <BODY>


    <TABLE name="tblCusInfo">
    <TR>
       <TD><FONT size=1 color=red></FONT>First Name</TD>
       <TD><INPUT name=txtFName style="HEIGHT: 22px; WIDTH: 100px" ></TD>
       <TD><FONT size=1 color=red>
    </FONT>Last Name</TD>
       <TD><INPUT name=txtLName style="HEIGHT: 22px; WIDTH: 100px" ></TD>
    </TR>
    <TR>
       <TD><FONT size=1 color=red>*</FONT>Age</TD>
       <TD><INPUT name=txtAge
         style="HEIGHT: 22px; WIDTH: 26px"></TD>
    </TR>


    </TABLE>
    <INPUT type=submit class=submit value="register" name=btnsubmit >


    </FORM>
    </BODY>
    </HTML>

  • 18 years ago

    Try this and see if this may help you out, I think you had an extra if statement...


       if(isNaN(frmCusInfo.txtAge.value))
       {
         alert("Invalid data format.\n\nOnly numbers can be entered for the Age field.");
         frmCusInfo.txtAge.focus();
         return (false);
       }


    It works for me...  




  • 18 years ago

    you could also try the parseFloat or parseInt functions to tet if the value is numeric or otherwise. then you would need to test whether the number was within the valid range.


    something like


     var age = parseInt(frmCusInfo.txtAge.value)


     if (isNaN(age)) {
       alert("Please enter numbers only");
       frmCusInfo.txtAge.focus();
       return false;
     }
     else if (age<0 || age>100) {
       alert("Please enter a valid age 0-100");
       frmCusInfo.txtAge.focus();
       return false;
     }



    you could combine the testing into one if statement, but that woud make displaying a useful error message difficult.


    nzjonboy

  • 18 years ago

    I agree with nzjonboy, his code would probably be safer to use since every field pulled using JavaScript is a string anyway.  It's best to parse it like he did first.




  • 18 years ago

    it's not a bad idea to use the parseInt() and parseFloat() and the others to convert all data to the type you want first. not strictly necessary but then it doesnt hurt?


    pity javascript doesnt have the 'As Integer' or 'As....' stuff, like VB does. i reckon vbscript would be better keeping that in, personally :P

  • 18 years ago

    paulfp,


    i hear your point about VBScript but not many browsers as yet support VBScript(still) this includes Netscape(does anyone still use it for anything other than testing if your code works on it?) and Opera(I like it).


    Its safer to use JavaScript as its got some support at least on major browsers.

  • 18 years ago

    oh quite, i fully agree! i ONLY ever use javascript on the client side, never vbscript. this is one of the many reasons i hate netscape so much. i mean if there are government guidelines for the quality of service you receive in an airport or at the butcher's, then why can't there be such things for software quality? the uk government is always fining railtrack for being so crap, why can't the us g'ment fine netscape for providing their users with such a second-rate internet experience and making our, the web developers', lives so hard!! hehehe


    anyway, back to my point.... i've never in my life actually uploaded and used a page with vbscript running on the client side, but it use vbscript lots in ASP, which rocks.

  • 18 years ago

    paul i gotta hand it to ya brother! A M E N!


    Making sites in 97-99 when Netscape 4.x was still running were hell for me...


    this is totally off topic but i just had to let my fustration out somewhere...


    Peace

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare