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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple validation 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
Hi,

I currently use the following to force a user to input data in a textbox

Code:
function EW_hasValue(obj, obj_type) {
	if (obj_type == "TEXT" || obj_type == "PASSWORD" || obj_type == "TEXTAREA" || obj_type == "FILE")	{
		if (obj.value.length == 0) 
			return false;		
		else 
			return true;
	}	else if (obj_type == "SELECT") {
		if (obj.type != "select-multiple" && obj.selectedIndex == 0)
			return false;
		else if (obj.type == "select-multiple" && obj.selectedIndex == -1)
			return false;
		else
			return true;
	}	else if (obj_type == "RADIO" || obj_type == "CHECKBOX")	{
		if (obj[0]) {
			for (i=0; i < obj.length; i++) {
				if (obj[i].checked)
					return true;
			}
		} else {
			return (obj.checked);
		}
		return false;	
	}
}

function EW_onError(form_object, input_object, object_type, error_message) {
	alert(error_message);									
	if (object_type == "RADIO" || object_type == "CHECKBOX") {
		if (input_object[0])
			input_object[0].focus();
		else
			input_object.focus();
	}	else if (!EW_isHTMLArea(input_object, object_type)) { 
		input_object.focus();  
	}  
	if (object_type == "TEXT" || object_type == "PASSWORD" || object_type == "TEXTAREA" || object_type == "FILE") {
		if (!EW_isHTMLArea(input_object, object_type))
			input_object.select();
	}
	return false;	
}

function EW_checkMyFormcontactus(EW_this) {
if (EW_this.x_phone && !EW_hasValue(EW_this.x_phone, "TEXT" )) {
	if (!EW_onError(EW_this, EW_this.x_phone, "TEXT", "Please enter a phone number..."))
		return false;
}
return true;
}

this is called from a submit image

Code:
<input name="Action" type="image" id="Action" value="ADD" src="images/messagebutbot_06.gif" width="44" height="18" border="0" alt="send" onClick="return EW_checkMyFormcontactus(this.form);">

but now i want to give users a choice of either a phone number or email or address...or a combination

the other text boxes will be called x_email and x_address

can you help me do this

 
This should do what you need :
Code:
function EW_checkMyFormcontactus(EW_this) {
if (EW_this.x_phone && !EW_hasValue(EW_this.x_phone, "TEXT" )) {
       return true;
}
if (EW_this.x_email && !EW_hasValue(EW_this.x_email, "TEXT" )) {
       return true;
}
if (EW_this.x_address && !EW_hasValue(EW_this.x_address, "TEXT" )) {
       return true;
}
alert("you must enter either a phone number or an email adress or a postal adress.");
return false;
}

Water is not bad as long as it remains outside human body ;-)
 
Hi - thanks for your replies

Dan - serverside takes longer because the page has to be posted, could use ajax for validation - does anyone use this technique for form validation ?

Targol - thanks for the code can you show me how to implement it within an existing function
have tried

Code:
function EW_checkMyFormcontactus(EW_this) {
if (EW_this.x_name && !EW_hasValue(EW_this.x_name, "TEXT" )) {
	if (!EW_onError(EW_this, EW_this.x_name, "TEXT", "Please enter a name..."))
		return false;
}

{
if (EW_this.x_phone && !EW_hasValue(EW_this.x_phone, "TEXT" )) {
       return true;
}
if (EW_this.x_email && !EW_hasValue(EW_this.x_email, "TEXT" )) {
       return true;
}
if (EW_this.x_address && !EW_hasValue(EW_this.x_address, "TEXT" )) {
       return true;
}
alert("you must enter either a phone number or an email adress or a postal adress.");
return false;
}
return true;
}
[code]
 
serverside takes longer because the page has to be posted

That is such a lame excuse. If you care more about that than your app working for everyone, or your app being more secure (because client-side validation is so easy to bypass with any DOM inspector), then that's up to you, I guess.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Validations on both sides have their legitimate purpose. I don't see why client-side validation is not a worthwhile functionality to investigate.
 
dont think its a 'lame excuse' - javascript validation is more widely used than server side depending on what your validating - if its a secure login page then i agree,
server side is a more secure option, but this is a general contact form for customers

as far as security goes the fields that are requested
are stripped of ' in case of sql injection attempts
 
>javascript validation is more widely used than server side depending on what your validating
If you put it that way, I may not agree neither!
 
come off it - honestly now, how many forms do you fill in that post the page then come back with errors v. js popups telling you where mistakes are made ?

im not saying js validation is better [i really am not] but, if you have a contact page with lots of content that needs to be posted every time someone clicks submit - js validation is quicker

bbc.co.uk uses js validation
 
You have an unuseful opening bracket without corresponding closing one.
As is, your corrected function should look like (I also simplified the first test and add a focus setting on the phone field if none of the 3 fields is filled) :
Code:
function EW_checkMyFormcontactus(EW_this) {
if (EW_this.x_name && !EW_hasValue(EW_this.x_name, "TEXT" )) {
    return EW_onError(EW_this, EW_this.x_name, "TEXT", "Please enter a name...");
}
if (EW_this.x_phone && !EW_hasValue(EW_this.x_phone, "TEXT" )) {
       return true;
}
if (EW_this.x_email && !EW_hasValue(EW_this.x_email, "TEXT" )) {
       return true;
}
if (EW_this.x_address && !EW_hasValue(EW_this.x_address, "TEXT" )) {
       return true;
}
alert("you must enter either a phone number or an email adress or a postal adress.");
EW_this.x_phone.select();
return false;
}

This should work. If no, tell me the line and the message of the error.

Water is not bad as long as it remains outside human body ;-)
 
>come off it
You come off it and then fall back in it. You don't have to be defensive, there is no need. Nobody knows whether you have server-side validation coupled with it or not. You being defensive only make thing undefensive. Are you saying bbc doesn't have any server-side validation on-top as well?
 
Hi tarjol - thanks for your reply

tried the code above - no errors, and does pick up the name field is empty but doesnt see that the other fields are empty

thanks again
 
-> thompon : what do you mean by "doesnt see that the other fields are empty" ?
The code is intended to check if the 3 fields are empty.
- If so, the first one is selected (can select the 3 ones as the same time)
- If one of them is filled, the function returns true and no one is selected.

Isn't it what you wanted ?

Water is not bad as long as it remains outside human body ;-)
 
Hi,

sorry if i wasnt being clear - at the moment if all three fields x_phone, x_email and x_address have a value then i get the message

you must enter either a phone number or an email adress or a postal adress.

but i need the func to return the message if none of the fields have a value

cheers MG
 
Sorry, it's my fault, I missed a "!" on each test. FYI, a "!" in javascript is a logical negation.

The good one :
Code:
function EW_checkMyFormcontactus(EW_this) {
if (EW_this.x_name && !EW_hasValue(EW_this.x_name, "TEXT" )) {
    return EW_onError(EW_this, EW_this.x_name, "TEXT", "Please enter a name...");
}
if (EW_this.x_phone && EW_hasValue(EW_this.x_phone, "TEXT" )) {
       return true;
}
if (EW_this.x_email && EW_hasValue(EW_this.x_email, "TEXT" )) {
       return true;
}
if (EW_this.x_address && EW_hasValue(EW_this.x_address, "TEXT" )) {
       return true;
}
alert("you must enter either a phone number or an email adress or a postal adress.");
EW_this.x_phone.select();
return false;
}

Water is not bad as long as it remains outside human body ;-)
 
thanks v much targol - heres a *

and thanks dan and tsuji for making me understand how important it is to validate server side - you have both helped me on numerous occasions and deserve my respect ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top