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
https://forums.phpfreaks.com/topic/137662-solved-multiple-validations-on-form/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.