Dummy needs a simple problem solved.

Discussion in 'Javascript' started by Blue®, Nov 3, 2003.

  1. Blue®

    Blue® Guest

    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="JavaScript1.2">
    function checkpostal(){
    var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
    if (document.myform.RecordID.value.search(re5digit)==-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="checkpostal()" value="check">
    </form>
    </body>
    </html>
     
    Blue®, Nov 3, 2003
    #1
    1. Advertising

  2. "Blue®" <> writes:

    > 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.


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

    However, then it will always submit. The "checkpostal" 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.
    > <html>
    > <head>
    > </head>
    > <body>
    >
    > <script language="JavaScript1.2">


    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.myform.RecordID.value.search(re5digit)==-1) {//if match failed
    alert("Please enter a valid 5 digit number.");
    return false;
    }
    return true;
    }
    > </script>
    >
    > <form name="myform" ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST">


    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="return checkpostal()">

    > <input type="text" name="RecordID" size=15>
    > <input type="button" onClick="checkpostal()" value="check">


    Just make it a submit button then:
    <input type="submit" value="check">

    > </form>
    > </body>
    > </html>


    Good luck.
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 3, 2003
    #2
    1. Advertising

  3. Blue®

    Blue® Guest

    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.myform.RecordID.value.search(re6digit)==-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="return 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>
     
    Blue®, Nov 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Meddows
    Replies:
    1
    Views:
    425
    John Saunders
    Jun 8, 2004
  2. C not so sharp

    Dummy needs help with MapPath

    C not so sharp, Jun 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    431
    clintonG
    Jun 11, 2004
  3. Michael Bacarella
    Replies:
    26
    Views:
    1,423
    harri
    Nov 20, 2007
  4. Michael Swift

    Javascript dummy needs help.

    Michael Swift, Jan 8, 2010, in forum: Javascript
    Replies:
    0
    Views:
    121
    Michael Swift
    Jan 8, 2010
  5. Replies:
    6
    Views:
    137
    Dennis Lee Bieber
    Jun 22, 2013
Loading...

Share This Page