Dummy needs a simple problem solved.

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

    #1

    Dummy needs a simple problem solved.

    I found this piece of JS which serves exactly what I need. It validates if
    the input is exactly five numberic characters.

    When it is validated, the form action is not carried out. What should I add
    to the code so that the form action is carried out?

    P/s: I do not know JS at all.

    =============== =============== ====
    <html>
    <head>
    </head>
    <body>

    <script language="JavaS cript1.2">
    function checkpostal(){
    var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
    if (document.myfor m.RecordID.valu e.search(re5dig it)==-1) //if match failed
    alert("Please enter a valid 5 digit number.")
    }
    </script>

    <form name="myform" ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST">
    <input type="text" name="RecordID" size=15>
    <input type="button" onClick="checkp ostal()" value="check">
    </form>
    </body>
    </html>


  • Lasse Reichstein Nielsen

    #2
    Re: Dummy needs a simple problem solved.

    "Blue®" <superbaby@myja ring.net> writes:
    [color=blue]
    > I found this piece of JS which serves exactly what I need. It validates if
    > the input is exactly five numberic characters.
    >
    > When it is validated, the form action is not carried out. What should I add
    > to the code so that the form action is carried out?[/color]
    [color=blue]
    > P/s: I do not know JS at all.[/color]

    Or HTML?
    Change <input type="button" ...> to <input type="submit" ...>

    However, then it will always submit. The "checkposta l" function
    doesn't return any value, so there is no way for the rest of the
    page to know whether the validation succeeded.

    Try this:

    Remeber to add a <!DOCTYPE> declaration. It is required for HTML 4.[color=blue]
    > <html>
    > <head>
    > </head>
    > <body>
    >
    > <script language="JavaS cript1.2">[/color]

    In HTML 4 and later, the "type" attribute is required. The "language"
    attribute is deprecated, and should be omitted. If it is included,
    don't use the version number 1.2 unless you know what the difference
    between 1.2 and 1.3 is, and in which browsers it makes a difference
    (i.e., just don't).
    <script type="text/javascript">

    function checkpostal(){
    var re5digit=/^\d{5}$/; //regular expression defining a 5 digit number
    if (document.myfor m.RecordID.valu e.search(re5dig it)==-1) {//if match failed
    alert("Please enter a valid 5 digit number.");
    return false;
    }
    return true;
    }[color=blue]
    > </script>
    >
    > <form name="myform" ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST">[/color]

    It is smarter to add validation to the submit event of the form, then
    it is checked no matter how the form is submitted.

    <form .... onsubmite="retu rn checkpostal()">
    [color=blue]
    > <input type="text" name="RecordID" size=15>
    > <input type="button" onClick="checkp ostal()" value="check">[/color]

    Just make it a submit button then:
    <input type="submit" value="check">
    [color=blue]
    > </form>
    > </body>
    > </html>[/color]

    Good luck.
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Blue®

      #3
      Re: Dummy needs a simple problem solved.

      Thanks, Lasse. It works!!!

      I spent the whole morning for it. Luckily you stepped in. The final code is
      as below:

      =============== =============== ====
      Lawrence Lam
      Keywords: Validate JS Javascripts input form digit digits number character
      characters
      =============== =============== ====

      <script type="text/javascript">

      function checkpostal(){
      var re6digit=/^\d{6}$/; //regular expression defining a 6 digit number
      if (document.myfor m.RecordID.valu e.search(re6dig it)==-1) {//if match
      failed
      alert("Please enter a valid 6-digit number.");
      return false;
      }
      return true;
      }
      </script>


      <FORM name=myform ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST"
      onsubmit="retur n checkpostal()">
      <b>Quick Machine Search<br>
      </b>Please enter <i>Record ID</i>:<br>
      <input type=text name="RecordID" size="16"><font color="#808080" > <br>
      </font>
      <input type=submit value="Search Machine"><hr color="#FF0000"
      size="1"></form>


      Comment

      Working...