Jump to content

[SOLVED] multiple validations on form


szejia

Recommended Posts

Hi i'm a complete newbie when it comes to javascript! Need help with adding on different kinds of functions to a validation script that i picked off the net.

 

I have the following fields in my form:

Name: [id=name]

Contact Number:[id=number]

Mailing Address:[id=add1]

Country:[id=country]  (this is a dropdown menu)

Postal Code:[id=code]

Email: [id=email]

 

i would like to make sure that:

1. 'name' is filled up

2. 'number' has 8 numbers

3. 'add1' is filled up

4. 'country' is selected (this is a drop down box)

5. 'code' has 6 numbers

5. 'email' is valid

 

 

currently i have a script that ensures that my checkbox is checked, and i just want to add on to this existing script.  it is as follows:

 

<script type="text/javascript">

 

function validate(form){

if (form1.tnc.checked == false )

{

alert('Please read and agree to our terms and conditions');

return false;

}else

return true;

}

 

</script>

 

 

Link to comment
Share on other sites

"How" are you wanting to validate the Country select value? Is the first optino something like "<--Select-->"?

 

<html>
<head>
<script>

function validEmail(emailStr)
{
    //Return true/false for valid/invalid email
    formatTest = /^[\w`\-=~!#$%^&*'+{}|'/?]+(\.[\w`\-=~!#$%^&*'+{}|'/?]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i
    lengthTest = /^(.{1,64})@(.{4,255})$/
    return (formatTest.test(emailStr) && lengthTest.test(emailStr));
}

function validPhone(fieldObj)
{
    var phone = fieldObj.value.replace(/[^\d]/g, '')
    if (phone.length !=  { return false; }
    fieldObj.value = phone;
    return true;
}

function validCode(fieldObj)
{
    var code = fieldObj.value.replace(/[^\d]/g, '')
    if (code.length != 6) { return false; }
    fieldObj.value = code;
    return true;
}

function validate(form1)
{
    var errors = new Array();

    //Validate name
    if (!form1['name'].value)
    {
        errors[errors.length] = 'Name is required.';
    }
    //Validate contact phone
    if (!validPhone(form1.number))
    {
        errors[errors.length] = 'Contact number must be 8 digits.';
    }
    //Validate mailing address
    if (!form1.add1.value)
    {
        errors[errors.length] = 'Mailing address is required.';
    }
    //Validate country
    if (!form1.country.value)
    {
        errors[errors.length] = 'Select a country.';
    }
    //Validate postal code
    if (!validCode(form1.code))
    {
        errors[errors.length] = 'Postal code must be 6 digits.';
    }
    //Validate email
    if (!form1.email.value)
    {
        errors[errors.length] = 'Email address is required.';
    }
    else if (!validEmail(form1.email.value))
    {
        errors[errors.length] = 'Email is not in a valid format.';
    }
    //Validate terms & conditions
    if (!form1.tnc.checked)
    {
        errors[errors.length] = 'Please read and agree to our terms and conditions.';
    }

    //There were errors
    if (errors.length!=0)
    {
        var msg = 'The following errors occured:';
        for (var i=0; i<errors.length; i++)
        {
            msg += '\n - ' + errors[i];
        }
        alert(msg);
        return false;
    }

    //No errors
    return true;
}


</script>

</head>
<body>
<form name="" onsubmit="return validate(this);">
Name: <input type="text" name="name" id="name" /><br>
Contact Number: <input type="text" name="number" id="number" /><br>
Mailing Address: <input type="text" name="add1" id="add1" /><br>
Country:
<select name="country" id="country" />

</select><br>
Postal Code: <input type="text" name="code" id="code" /><br>
Email: <input type="text" name="email" id="email" /><br>
<input type="checkbox" name="tnc" id="tnc" /> Accept terms<br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.