#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    44
    Rep Power
    14

    Unhappy Javascript and Form Validation


    Here's the script in the Head section of the HTML document.

    <Script Language="Javascript">
    function validate() {
    if (document.Theory.ContentQuality.value != 'e'||'f'||'g'||'p'||'E'||'F'||'G'||'P') {
    alert("Please enter E, F, G, or P for Question 1.");
    return false;

    }
    else {
    if (document.Theory.UnderstandRate.value != 'e'||'f'||'g'||'p'||'E'||'F'||'G'||'P')
    alert("Please enter E, F, G, or P for Question 2.");
    return false;

    }
    return true;
    }
    </script>


    Here's the line that calls the validation function in the BODY of the HTML document.

    <input type="text" name="ContentQuality" maxlength="1" size="2" value="" onChange="validate();javascript:this.value=this.value.toUpperCase();"return document.MM_returnValue">


    What it should do?

    Whenever the user types characters other than e,f,g,p,E,F,G,or P they should see the alert message. However, the user can type any character and the alert message displays. Also, it continues to the next field. I want it to stop on the incorrect input. The user should not advance to the field if their input is invalid.

    What can I do to clean this script up. I have tried several alternatives.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Location
    about:blank
    Posts
    26
    Rep Power
    0

    try this


    <head>
    <Script Language="Javascript">
    function validate() {

    if (/[EFGP]+/g.test(document.Theory.ContentQuality.value.toUpperCase())<=0) {
    alert("Please enter E, F, G, or P for Question 1.");
    document.Theory.ContentQuality.value="";
    document.Theory.ContentQuality.focus(); }
    }

    function validateAll() {
    if (/[EFGP]+/g.test(document.Theory.UnderstandRate.value.toUpperCase())<=0) {
    alert("Please enter E, F, G, or P for Question 2.");
    document.Theory.UnderstandRate.value="";
    document.Theory.UnderstandRate.focus(); }
    else
    if(/[EFGP]+/g.test(document.Theory.ContentQuality.value.toUpperCase())<=0) {
    alert("Please enter E, F, G, or P for Question 1.");
    document.Theory.ContentQuality.value="";
    document.Theory.ContentQuality.focus(); }
    }
    </script>
    </head>
    <body>
    <form NAME="Theory">
    ContentQuality:<input type="text" name="ContentQuality" maxlength="1" size="2" value=""><br>
    UnderstandRate:<input type="text" name="UnderstandRate" maxlength="1" size="2" value="" onFocus="validate();"><br>
    <input type="submit" name="ok" value="ok" OnFocus="validateAll();">
    </form>
    </body>


    Also note that it's still possible to send invalid
    values to wherever it is that this form is targeted to
    via the Url ie:
    typing into the addy bar:
    www.bla.com/yourScript.php?&UnderstandRate=XX&ContentQuality=YY
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16

    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    function check(field, which) {
    if (!/^[efgp]$/i.test(field.value)) {
    var msg = 'In answer to ' + which + ' - \nPlease enter one of the following:\n';
    msg += '-----------------------------------------\ne, f, g, p (or E, F, G, P)\n';
    msg += '-----------------------------------------\n\nThanks!';
    alert(msg);
    field.focus();
    field.select();
    return false;
    }
    }

    </script>
    </head>
    <body>
    <form>
    <h4>Question 1</h4>
    <input type="text" name="ContentQuality" maxlength="1" size="2"
    onblur="return check(this,'Question 1')" />
    <h4>Question 2</h4>
    <input type="text" name="UnderstandRate" maxlength="1" size="2"
    onblur="return check(this,'Question 2')" />
    </form>
    </body>
    </html>
    Last edited by adios; September 15th, 2002 at 09:36 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    44
    Rep Power
    14

    Thanks Adios


    Adios:

    That script worked. I finally got the validation to work as needed. Again thanks.

IMN logo majestic logo threadwatch logo seochat tools logo