Contact form validation

design92

New Member
Hi every1,

First of all, I have no idea how to create javascript.

I have to create contact form (I have already built it, any1 know why i cannot attach it?) validated by JavaScript following client requirments as shown below:

Code:
First Name- 
Mandatory – make sure that an entry has been made in this field.
 
 
Second Name – 
Mandatory - – make sure that an entry has been made in this 
field.
 
House number or name – Mandatory – 
make sure that an entry has been made in this field.
 
 
Street name – Mandatory  
- – make sure that an entry has 
been made in this field.
 
 
Town – Mandatory - – 
make sure that an entry has been made in this field.
 

Post Code – Mandatory - – 
make sure that an entry has been made in this field.
 
 
Mobile – Must have an entry if Home is blank, if an entry has 
been made strip out all non-numeric characters.

Home – Must have an entry if Mobile is blank, if an entry has 
been made strip out all non-numeric characters.
 
 
Email –                                                 
Mandatory,  plus check the 
format of the email address entered – it must contain an @ symbol at some point 
after the first character followed by a dot at some point after the third 
character – the dot cannot be the last character.
 
 
 
Newspapers Read – if one of the choices is ‘Other’  
then the ‘State one other Newspaper you read’ field becomes 
mandatory.
 
 
State one other Newspaper you read –Mandatory if the ‘Other’ 
option is chosen in the Newspapers Read field.
 
 
Comments – Mandatory and must contain at least 15 
characters.
 
 
When the Submit button is clicked the JavaScript will call a function which will 
validate the entries made on the form.
A validation with errors will issue an appropriate JavaScript alert message to 
indicate fields in error. The cursor will also be focussed on the first field in 
error.
A validation with no errors will issue a JavaScript alert message to read ‘Thank 
you for your enquiry’.
 
When the Reset button is clicked all fields in the Form will be cleared of entries 
and the cursor will be focussed back to the first field at the top of the form

I know it could be validated by PHP much more simple, but JavaScript is necessary.
Any1 know why I cannot attach any files?

All the best,
Design92
 
Last edited:

design92

New Member
If I would like to use jQuery or other plugins, I would use it as now I understand it. Before starting with jQuery I had some code in JavaScript (normal one, not plugin), and found some tutorials so I have managed to do validate my form without using jQuery. Now, this is the question: I have mobile, and home fields. They are dependent. I have managed to make dependencies so if mobile is blank, home must be filled in, and the other way round. Now - I have also managed to make validation on these two fields so only numerical characters can be typed in. But my issue is, once form is submitted it accepts even 1 digits, as it should validate so there is 11 digits typed in. I have set max lenght of the field in field properties, but I DO NOT know how to set min length. Anyone can help?
This is the code I have done but Dreamweaver pops up a syntax error on line 86 which is:

Code:
else if (document.mobile.value.length >< 11)

This is the full validation code for mobile and home:

Code:
else if (mone == "" && mtwo == "")

{
document.getElementById('mobile').focus();
alert("Please fill at least one of the contact number.");
return false;
}

else if (document.mobile.value.length >< 11)
{
	alert("Telephone number has to be 11 digits.");
	return false;
}

What is wrong with it?
 

CaldwellYSR

Member
you're saying if the length is less than greater than eleven... of course it's a syntax error because that's an impossible condition. You're trying to say "not equal to" 11 which is done with "!=" ! means not
 

design92

New Member
Right so how many code should look like as I'm confused with your explanation :)

I have also found some examples of validating length and also format so do you have any idea how I could combine these two into one code so it will validate for 11 digits, and numerical characters only?

checking for numbers only

Code:
<script type='text/javascript'>
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
</script>
<form>
Numbers Only: <input type='text' id='numbers'/>
<input type='button' 
	onclick="isNumeric(document.getElementById('numbers'), 'Numbers Only Please')"
	value='Check Field' />
</form>

restricting the length

Code:
<script type='text/javascript'>
function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}
</script>
<form>
Username(6-8 characters): <input type='text' id='restrict'/>
<input type='button' 
	onclick="lengthRestriction(document.getElementById('restrict'), 6, 8)"
	value='Check Field' />
</form>


@edit

Ok, I got rid of syntax, but it is not validating as required. Even when I type in 1 numeric character, it validates it as correct. I have few another cells after mobile, such as email, comments, tickbox etc and it is not validated as it used to be validated before getting rid of syntax. It just validates the values up to mobile, and after 1 character is typed into mobile it just sents of the form. Do you know why?

@edit 2

I used to get javascript alert - thank you for your enquiry once form was submitted and now I don't.
 
Last edited:

CaldwellYSR

Member
Dude if you have no idea how to write javascript then you shouldn't have accepted a job from a client that required javascript validation. You can't just go around pasting together bits of code taht amek nose sense to you and hoping they will work. I explained the simple syntax error you were having in VERY layman's terms and it confused you. That's a simple logical operator that you don't understand and you're expecting to put together form validation. Give up and outsource this job to someone else. Then if you want to learn javascript learn it from the beginning.
 

design92

New Member
Well, I just misunderstood your message, but I've managed to do that as I said. I just wonder how to put two codes into one. Is that so hard for you to help me? I had validated whole contact form exempt mobile length and numerics only. I came here seeking for help, not for criticism. If you know how to do that, why can't you help me?

This is my current validation for mobile field:

Code:
<script type='text/javascript'>
function lengthRestriction(elem, min, max){
        var uInput = elem.value;
        if(uInput.length >= min && uInput.length <= max){
                return true;
        }else{
                alert("Please enter " +max+ " characters");
                elem.focus();
                return false;

        }
}
</script>

<form>
Username(6-8 characters): <input type='text' id='restrict' maxlength="11"/>
<input type='button' 
        onclick="lengthRestriction(document.getElementById('restrict'), 11, 11)"
        value='Check Field' />
</form>

How do I add validation for numeric characters only PLEASE?
 
Top