Make the fields mandatory

Discussion in 'HTML & Website Design' started by Brooke.Coin, Jan 18, 2009.

  1. #1
    Hi,
    I have a landing page that colect name and email.
    How to do I make the filed mandatory?
    Would be happy to get your help.

    Tnx
    Brooke
    P.S.: I attached most of the source code on the page.

    ------------
    <body>
    <script>
    function valid_required(field)
    {
    if(field=="") {
    return false;
    }
    return true;
    }

    function valForm()
    {
    if(!valid_required(document.myform.email.value))
    {
    alert("Please fill out all the fields")
    return false;
    }
    return true;
    }

    </script>
    <tr>
    <td>
    <form method="post" name="myform" onSubmit="return valForm()" action="http://mydomian/index.php">
    <p align="right"><font color="darkmagenta"><u><b>Please leave your name and phone number</b></u><br>
    <b><font color="mediumvioletred">First Name:
    <input type="text" size="23" name="First_name0">
    Phone Number: <input type="text" size="26" name="Phone_Number1"></p>
    <center>
    <input type="submit" name="submit" value="More Info" style="font-weight: 700">
    </center></div>
    </form>
    </font></b></font></td>
    </tr>
    </table>
    </font></b></font>

    </body>
     
    Brooke.Coin, Jan 18, 2009 IP
  2. katendarcy

    katendarcy Peon

    Messages:
    115
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you have a field named "email"? I see you're checking it but I don't see it on the form. I tested it and switched it to "Phone_Number1", which worked. Let me know if that was the problem for you.
     
    katendarcy, Jan 18, 2009 IP
  3. webvinay

    webvinay Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    any one can disable your validation by just disabling javascript ... since you r using php therefore you can use server side validation ..... i think that will be better for you
     
    webvinay, Jan 19, 2009 IP
  4. Brooke.Coin

    Brooke.Coin Peon

    Messages:
    113
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    WORKED!!!
    Thank you katendarcy.
    Webvinay - it depends on your visitors. I don't promote to internet savvy.

    Brooke
     
    Brooke.Coin, Jan 19, 2009 IP
  5. katendarcy

    katendarcy Peon

    Messages:
    115
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Glad it worked for you. I wasn't sure if you just hadn't posted all of the fields, or if it really was the problem. : )

    I'm not sure what else you have planned for the validation, but I was thinking about this after I posted last night and wanted to make a suggestion. If you do have multiple fields on a form, it's often a better experience for the user if you just tell them upfront what they need to fix. (That's my opinion, at least.) That way, you just have one alert for all the errors. If you want to do that, something like this would work:

    /*NOTE: I use a different method for getting the values of input fields. But, you could still use "document.myform.....". Also, I just used the check you're currently using.*/


    function valForm(){

    var txtField1;
    var txtField2;
    var strError;

    txtFName = document.getElementById('txtFName');
    txtLName = document.getElementById('txtLName');

    strError = "";
    strErrorMsg = "You must fix the following errors before proceeding:\n";

    if (txtField1.value==""){
    strError = strError + " - Please enter a first name.\n";
    }

    if (txtField2.value==""){
    strError = strError + " - Please enter a last name.\n";
    }

    if (strError.value=="" || strError.length == 0){
    //do nothing...
    }
    else{
    strErrorMsg = strErrorMsg + strError;
    alert(strErrorMsg);
    return false;
    }

    }

    Personally I use JS and PHP for validation. The JS to save the server and for those that have it running, and the PHP as a fall back and for validation the client can't do. But, it is up to you. Take care! : )
     
    katendarcy, Jan 19, 2009 IP