Need Help With Numeric Validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSDunn

    #1

    Need Help With Numeric Validation

    Hello,
    I need to apply validation to two text boxes. One text box named
    FirstNumber is required to contain only numbers, and be only three
    digits long.

    The other text box named SecondNumber needs to also require only
    numbers, but allow for four digits.

    I need to have a msg box appear after the user clicks a button named
    'submit' if the validation on either text box has been violated.

    I spend most of my time writing SQL, and I'm not well versed at all
    with JavaScript.

    Please help!

    Thanks!

    CSDunn
  • Evertjan.

    #2
    Re: Need Help With Numeric Validation

    CSDunn wrote on 08 mei 2004 in comp.lang.javas cript:
    [color=blue]
    > I need to apply validation to two text boxes. One text box named
    > FirstNumber is required to contain only numbers, and be only three
    > digits long.
    >
    > The other text box named SecondNumber needs to also require only
    > numbers, but allow for four digits.
    >
    > I need to have a msg box appear after the user clicks a button named
    > 'submit' if the validation on either text box has been violated.
    >[/color]

    <form onsubmit="retur n validate(this)" >
    <input name="b1">
    <input name="b2">
    <input type=submit>
    </form>


    <script>
    function validate(x){
    err1=(/^\d{1,3}$/.test(x.b1.valu e))?"":"first box error"
    err2=(/^\d{1,4}$/.test(x.b2.valu e))?"":"second box error"
    if(err1+err2!=" "){
    alert(err1+"\n" +err2)
    return false
    }
    return true
    }
    </script>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Ron

      #3
      Re: Need Help With Numeric Validation

      CSDunn wrote:
      [color=blue]
      >Hello,
      >I need to apply validation to two text boxes. One text box named
      >FirstNumber is required to contain only numbers, and be only three
      >digits long.
      >
      >The other text box named SecondNumber needs to also require only
      >numbers, but allow for four digits.
      >
      >I need to have a msg box appear after the user clicks a button named
      >'submit' if the validation on either text box has been violated.
      >
      >I spend most of my time writing SQL, and I'm not well versed at all
      >with JavaScript.
      >
      >Please help!
      >
      >Thanks!
      >
      >CSDunn
      >
      >[/color]
      The textbox maxlength can be taken care of through HTML:
      <form action="myHandl er.php" method="post" onsubmit="retur n validateForm()" >
      <label for="FirstNumbe r">First Number: </label>
      <input id="FirstNumber " name="FirstNumb er" type="text" size="5"
      maxlength="3" />
      <br />
      <label for="SecondNumb er">Second Number: </label>
      <input id="SecondNumbe r" name="SecondNum ber" type="text" size="5"
      maxlength="4" />
      <br />
      <button type="submit">S ubmit Form</button>
      </form>
      Somewhere in your head:
      <script type="text/javascript">
      function validateForm() {
      var onlyNums = new RegExp(/^\D+$/); // matches one or more non-digits.
      var firstString = document.forms[0].elements["FirstNumbe r"].value;
      if((onlyNums.te st(firstString) ) || (onlyNums.test( secondString))) {
      alert("You can only use numbers in this form.");
      return false;
      }
      else {
      return true;
      }
      }
      </script>
      Javascript uses Perl-type regular expressions.

      Comment

      • Ron

        #4
        Correction: Re: Need Help With Numeric Validation

        Ron wrote:
        [color=blue]
        > <script type="text/javascript">
        > function validateForm() {
        > var onlyNums = new RegExp(/^\D+$/); // matches one or more non-digits.
        > var firstString = document.forms[0].elements["FirstNumbe r"].value;[/color]

        var secondString = document.forms[0].elements["SecondNumb er"].value;
        [color=blue]
        >
        > if((onlyNums.te st(firstString) ) || (onlyNums.test( secondString))) {
        > alert("You can only use numbers in this form.");
        > return false;
        > }
        > else {
        > return true;
        > }
        > }
        > </script>[/color]

        I realized I didn't declare secondString as a variable, but you get the
        idea. :)

        Comment

        • Ron

          #5
          Re: Need Help With Numeric Validation

          Ron wrote:
          [color=blue]
          > CSDunn wrote:
          >[color=green]
          >> Hello,
          >> I need to apply validation to two text boxes. One text box named
          >> FirstNumber is required to contain only numbers, and be only three
          >> digits long.
          >> [snip][/color]
          >
          > [snip]
          > <script type="text/javascript">
          > function validateForm() {
          > var onlyNums = new RegExp(/^\D+$/); // matches one or more non-digits.
          > var firstString = document.forms[0].elements["FirstNumbe r"].value;
          > if((onlyNums.te st(firstString) ) || (onlyNums.test( secondString))) {
          > alert("You can only use numbers in this form.");
          > return false;
          > }
          > else {
          > return true;
          > }
          > }
          > </script>
          > Javascript uses Perl-type regular expressions.[/color]

          Hmm, due to the nature of the problem, instead of the regular expression
          test, you could also use:

          if((parseInt(fi rstString, 10).toString(10 )!=firstString) || ...) {
          ...
          }

          :)

          Comment

          • Dr John Stockton

            #6
            Re: Need Help With Numeric Validation

            JRS: In article <807dbff7.04050 71417.478c9f1b@ posting.google. com>, seen
            in news:comp.lang. javascript, CSDunn <cdunn@valverde .edu> posted at Fri,
            7 May 2004 15:17:35 :
            [color=blue]
            >I need to apply validation to two text boxes. One text box named
            >FirstNumber is required to contain only numbers, and be only three
            >digits long.[/color]

            You mean only decimal digits. Untested, but probably right :-

            S = ...FirstNumber. value
            OK = /^\d\d\d$/.test(S)
            if (!OK) { ... }
            [color=blue]
            >The other text box named SecondNumber needs to also require only
            >numbers, but allow for four digits.[/color]

            S = ...SecondNumber .value
            OK = /^\d{4}$/.test(S)
            if (!OK) { ... }


            <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Chris Dunn

              #7
              Re: Need Help With Numeric Validation

              My thanks to everyone for your help!

              CSDunn


              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              Working...