Form check problem

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

    Form check problem

    Hello,

    I add a javascript check to each input field. I use an onBlur event. But I
    want to add a "batch check" function to a onSubmit event too. In case of
    onBlur the call my check like this:
    onBlur="isValid Int(this,'Error text',false)

    But how can I call in case of onSubmit. There I can't use the "this" and the
    following code doesn't work:
    isValidInt(docu ment.ageField,' Error text', false);
    ....

    function isValidInt(user Input, errorMsg, isReq){
    var mask=/^\d+$/;
    if (isReq == false && userInput.value == "") return true;
    if (mask.test(user Input.value)) return true;
    return false;
    }

    Thanks!


  • Robert

    #2
    Re: Form check problem

    [color=blue]
    > function isValidInt(user Input, errorMsg, isReq){
    > var mask=/^\d+$/;
    > if (isReq == false && userInput.value == "") return true;
    > if (mask.test(user Input.value)) return true;
    > return false;
    > }[/color]
    I suspect that you need to reference the variable userInput.value by its
    full name.

    Here is an example:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>form check</title>

    <script type="text/javascript">

    function validate()
    {
    alert("in function validate.");
    var submitOK = true;

    // Notice how I referenced the fields in a form.
    // You do not need to make a separate variable.

    var theName = document.forms["myForm"].theName.value;
    var theEmail = document.forms["myForm"].theEmail.value ;

    alert(" theName = " + theName +
    " theEmail = " + theEmail);

    // I changed submitOK to a boolean variable.
    var submitOK = true;

    // Validate email: name and email

    if (theName == '')
    {
    alert("Please fill in your Name");
    submitOK = false;
    }
    if (theEmail == '')
    {
    alert("Please fill in Email");
    submitOK = false;
    }

    return submitOK;

    }

    </script>

    </head>
    <body>
    <p>Please try out our form.</p>
    <form name="myForm"
    action="http://www.nonamedomai n.com"
    method="POST"
    onsubmit="alert ('checing then submitting...') ;return validate();">
    <p>Name:<br>
    <input type="text" name="theName" size="20"><br>< br>
    Email:<br>
    <input type="text" name="theEmail" size="20"><br>
    </p>
    <p><input type="submit" value="Submit"> </p>
    </form>
    </body>
    </html>


    Robert

    Comment

    Working...