Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This Validation JS works only on IE. Plz Help 2

Status
Not open for further replies.

Qandil

Technical User
Sep 19, 2008
5
GB
Hi everyone,

I tried and tried, and search Google for two days, still doesnt work, Can you help me finding out why it doesnt work anywhere other than IE?

Thanks

Code:
<script type='text/javascript'>

//

function isEmpty(elem, helperMsg) {
	if (elem.value.length === 0) {
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return true;
	}
	return false;
}

function isNumeric(elem, helperMsg) {
	var numericExpression = /^[0-9]+$/;
	if (elem.value.match(numericExpression)) {
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg) {
	var alphaExp = /^[a-zA-Z]+$/;
	if (elem.value.match(alphaExp)) {
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg) {
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if (elem.value.match(alphaExp)) {
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

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;
	}
}

function madeSelection(elem, helperMsg) {
	if (elem.value === "Please Choose") {
		alert(helperMsg);
		elem.focus();
		return false;
	} else {
		return true;
	}
}

function emailValidator(elem, helperMsg) {
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if (elem.value.match(emailExp)) {
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

//
function formValidator() {
	// Make quick references to our fields
	var firstname = document.getElementById('firstname');
	var secondname = document.getElementById('secondname');
	var contactemail = document.getElementById('contactemail');
	var telnumber = document.getElementById('telnumber');
	var adultnumber = document.getElementById('adultnumber');
	var childnumber = document.getElementById('childnumber');
	var area = document.getElementById('area');
	var dep_day = document.getElementById('dep_day');
	var dep_month = document.getElementById('dep_month');
	var dep_year = document.getElementById('dep_year');
	var ret_day = document.getElementById('ret_day');
	var ret_month = document.getElementById('ret_month');
	var ret_year = document.getElementById('ret_year');
	
	// Check each input in the order that it appears in the form!
	if (isAlphabet(firstname, "Please enter only letters for your first name")) {
		if (isAlphabet(secondname, "Please enter only letters for your second name")) {	
			if (emailValidator(contactemail, "Please enter a valid email address")) {
				if (isNumeric(telnumber, "Please enter numbers only for the phone number")) {
					if (madeSelection(adultnumber, "Please Choose the number of adults")) {
						if (madeSelection(childnumber, "Please Choose the number of children")) {
							if (madeSelection(area, "Please Choose Area of Interest")) {
								if (madeSelection(dep_day, "Please Choose the departure day")) {
									if (madeSelection(dep_month, "Please Choose the departure month")) {
										if (madeSelection(dep_year, "Please Choose the departure year")) {
											if (madeSelection(ret_day, "Please Choose the return day")) {
												if (madeSelection(ret_month, "Please Choose the return month")) {
													if (madeSelection(ret_year, "Please Choose return year")) {
														return true;
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	
	return false;
	
}


</script>

</head>
<body>

	<form name="dest" method="POST" action="contactsend.php" onsubmit="return formValidator('dest');">
 
1. Can you please explain why you are saying it doesn't work in any other browser? What is the error you are getting?

2. Am I to assume that all the fields you are validating are text boxes? Knowing the form elements may help us out as well.

Thanks!
 
Thanks for answering,

Its just not validating, even if i leave the fields empty it takes me to the next page and sends the email.

They are text fields, number field, email field, and drop down selection

Thanks

 
[1] My guess is that you should have had defined name for each form field element. However, you systematically use id to refer to them. This works only in ie and that's why.

[2] Also you can make good use of the variable passing of the form object itself to make thing very precisely meant for the elements within that form and that it would be less dependent on the concrete form name.

[3]
> <form name="dest" method="POST" action="contactsend.php" onsubmit="return formValidator('dest');">
[tt] <form name="dest" method="POST" action="contactsend.php" onsubmit="return formValidator([red]this[/red]);">[/tt]
[4]
[tt]function formValidator([red]obj[/red]) {
// Make quick references to our fields
var firstname = [red]obj.elements['firstname'][/red];
//etc etc
}
[/tt]
 
[4.1] Also I see farther below you use firstname etc as scalar value, hence, I would prefer to define them all with .value explicitly even though common browsers all are happily convert them to its default value property.
>[self] var firstname = obj.elements['firstname'];
[tt] var firstname = obj.elements['firstname'][red].value[/red];[/tt]
 
Thank you very much tsuji, this has fixed it and its successfully validating on all browsers i've tried.

Best,
Qandil
 
Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

* [navy]Thank tsuji
for this valuable post![/navy]


at the bottom of tsuji's post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
 
Thanks, Feherke! that's too kind. I would keep it low, besides the op has thanks the forum, it is fine for me. (My only wish is people not flame back on me too severly when I flame somebody...yes, I would be thankful!)
 
Hi again :)

I wanted to add a checkbox validation, but i dont seem to know how to implement it with the above code. The code i want to add is
Code:
function checkCheckBoxes(elem, helperMsg) {
	if (elem.value == checked){ 
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
 
>if (elem.value == checked){
If elem is a checkbox, it is not the value - it is the checked property.
[tt]if (elem.[red]checked[/red] == [red]true[/red]){ [/tt]
or equivalently and more concisely
[tt]if (elem.[red]checked[/red]){ [/tt]
 
I agree with Feherke... I learned something from tsuji, so you have one
star.gif
from me :)
 
I think another star from me again.

You are amazing. I will make sure i ask a question everytime i have.

Thanks alot tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top