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 5 of 5

Thread: Form Validation

  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form Validation

    Hola,

    Before i submit a form, i would like to check if all the fields are filled and also i would like to check if my e-mail is entered in a proper format.
    It checks for the name and the text but it does'nt check for the e-mail.
    Could somebody please tell me where i'm going wrong.

    Many thanks

    PHP Code:
    <html>

    <
    head>
    <
    meta http-equiv="Content-Language" content="en-us">
    <
    meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <
    meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <
    meta name="ProgId" content="FrontPage.Editor.Document">
    <
    title>Contact Page</title>
    <
    SCRIPT LANGUAGE="JavaScript">
    function 
    verify() {
    var 
    themessage "You are required to complete the following fields: ";
    if (
    document.form.name.value=="") {
    themessage themessage " - Your Name";
    }
    if (/^
    w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(document.form.email.value)){

    themessage themessage " - Your E-mail";
    }
    if (
    document.form.description.value=="") {
    themessage themessage " -  Your Description";
    }
    //alert if fields are empty and cancel form submit
    if (themessage == "You are required to complete the following fields: ") {
    document.form.submit();
    }
    else {
    alert(themessage);
    return 
    false;
       }
    }
    </script>

    </head>

    <body bgcolor="#FFFFCC">
    <form  name=form method="post" action="contactform.html">
    Fields with '<font color="#ff0000">*</font>' are mandatory. 
    <table border="1" width="100%">
      <tr>
        <td width="50%">Your Name <font color="#FF0000">*</td>
        <td width="50%">
          <p><input type="text" name="name" size="20"></p>
          </td>
      </tr>
      <tr>
        <td width="50%">Your E-mail <font color="#FF0000">*</td>
        <td width="50%"><input type="text" name="email" size="20"></td>
      </tr>
      <tr>
        <td width="50%">Your Description <font color="#FF0000">*</td>
        <td width="50%">
          <p><textarea rows="7" name="description" cols="60"></textarea></p>
          
        </td>
      </tr>
    </table>

    <table border="1" width="100%">
      <tr>
        <td width="100%"><input type=button value="Submit" onclick="verify();"></td>
      </tr>
    </table>
    </form>
    </body>

    </html> 

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your regex matches a valid email address, so you need to 'flip' the value returned by the .test() method:

    if (!/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(document.form.email.value)){

    Might want to provide better feedback for your users on no email vs. 'bad' email addresses. Check the site search here.

    cya, cbp

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi ya cbp,

    That has'nt worked either.Though i give a valid e-mail address, it always give me the mesg, enter the e-mail.

    Any ideas why??

    Thanks

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.form.email.value)) {

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if it doesn't complain when you give it a bad e-mail then do what cheeseabagpipe says, if not we need to see the code.


  •  

    Posting Permissions

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