Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Validate all textboxes to be numeric

    I have a varying number of text boxes on a page... i want to validate that they all contain only 0-9 (numeric) and if so allow the form to submit

    thanks!

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,835
    Thanks
    21
    Thanked 157 Times in 148 Posts
    try:

    Code:
     <form name='form1' method='post' action='file.php' onsubmit="return validate(document.form1);">
    <input type='text' name='field1' />
    <input type="submit" name="submit" value="submit" />
    </form>
    
    <script type="text/javascript">
    
    function validate(formObj)
    {
    regex = /^\d+\.*\d*$/;
    if (!regex.test(formObj.field1.value))
    {
       alert('textbox does not contain a number');
       return false;
    }
    else
    {
       return true;
    }
    }
    
    </script>
    Last edited by chump2877; 02-05-2007 at 07:23 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    well while that looks like it might validate one textbox im sure theres a way to validate the DOM array that holds all of the textboxes... i just dont know how to do that and cant hardcode this since there are varying number of textboxes

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,835
    Thanks
    21
    Thanked 157 Times in 148 Posts
    give that a shot then...note: I'm not validating for anything other than numeric values in the textboxes....this also requires that the "submit" input be the very last input node in your form:

    Code:
     
    <form name='form1' method='post' action='file.php' onsubmit="return validate(document.form1);">
    <input type='text' name='field1' /><br />
    <input type='text' name='field2' /><br />
    <input type='text' name='field3' /><br />
    <input type='text' name='field4' /><br />
    <input type="submit" name="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function validate(formObj)
    {
     var inputArr = formObj.getElementsByTagName('input');
     var regex = /^[0-9]+$/;
     for (i=0; i<inputArr.length - 1; i++)
     {
      if (!regex.test(inputArr[i].value))
      {
       alert('textbox does not contain a number');
       inputArr[i].select();
       return false;
      }
     }
     return true;
    }
    </script>
    Last edited by chump2877; 02-05-2007 at 07:59 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #5
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    that pretty snappy!

    would it be possible to change the background-color: property of the offending text box after calling select with something like this:

    Code:
    inputArr[i].style.backgroundcolor='red';
    which doesnt work

  • #6
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    C of backgroundColor must be uppercase:

    inputArr[i].style.backgroundColor='red';

  • #7
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,835
    Thanks
    21
    Thanked 157 Times in 148 Posts
    Code:
    <form name='form1' method='post' action='file.php' onsubmit="return validate(document.form1);">
    <input type='text' name='field1' /><br />
    <input type='text' name='field2' /><br />
    <input type='text' name='field3' /><br />
    <input type='text' name='field4' /><br />
    <input type="submit" name="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function validate(formObj)
    {
     var inputArr = formObj.getElementsByTagName('input');
     var regex = /^[0-9]+$/;
     for (i=0; i<inputArr.length - 1; i++)
     {
      if (inputArr[i].style.backgroundColor=='red')
          inputArr[i].style.backgroundColor='';
     }
     for (i=0; i<inputArr.length - 1; i++)
     {
      if (!regex.test(inputArr[i].value))
      {
       alert('textbox does not contain a number');
       inputArr[i].select();
       inputArr[i].style.backgroundColor='red';
       return false;
      }
     }
     return true;
    }
    </script>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #8
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hrm... in firefox at least that doesnt work

  • #9
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,835
    Thanks
    21
    Thanked 157 Times in 148 Posts
    Works great for me in IE and Firefox...

    But the selection color in firefox certainly doesn;t complement a background color of red...

    But you get what you ask for
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #10
    Regular Coder
    Join Date
    Sep 2004
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hrm.. it does seem to work now.... thinking i forgot to save in jedit

    much obliged!

    fortunately i do not have a red BG color for the page


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •