Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macintosh basic validating empty fields (baby steps)

    beginner starting off, with very basic validating an empty field, to show error message if left empty, on submit.
    please help with basic newbie help.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>2mail.html</title>

    <script language="text/javascript" type="text/javascript">

    function validateForm(){
    var FirstName=document.MailForm.FirstName.value;

    //check if it is empty
    if(FirstName==""){
    //display error msg
    alert("Error: Please enter your first name");
    //stop user to continue
    return false;
    }
    //else
    else{
    //continue
    return true;
    }
    }


    </script>
    </head>

    <body>
    <form onSubmit="return validateForm();"
    action=""
    name="MailForm"
    enctype="text/plain">
    Name:
    <input type="text"
    name="FirstName"
    size="25"
    maxlength="25"/>
    <br>
    <br>
    <input type="submit"
    value="SubmitNow"/>
    <input type="reset"
    value="ClearForm"/>


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

  • #2
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is usually what I do:

    Code:
    <!--HTML:-->
    <form onsubmit="return validateForm(this);" ......
    <input name="field1" type="text" .....
    
    //JAVASCRIPT:
    function validateForm(form)
    {
       var error = "";
       if (form.name == "")
       {
           error += "You have to enter a name";
       }
    
       if (error)
       {
           alert(error);
           return false;
       }
       // Everything is ok! Send the form
       return true;
    }
    And just use <script type="text/javascript">. You don't need that language stuff...
    Drømmejenta: Kristen jente som progger Python!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •