Click here to Skip to main content
15,881,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
we are submitting a registration form, where in the user has to input the details for that we are providing certain alerts.Now the question is instead of using many ifelse loops can we supress the code for checking the validations.(i think we can do using for loop) Would be happy if any anyone puts the code

JavaScript
function validateFields() {

	valid = true;
	if (isEmpty(frm.id)) {
		alert("ID cannot be empty");
		valid = false;

	} else if (isEmpty(frm.name)) {
		alert("Name cannot be empty");
		valid = false;

	}

	else if (isEmpty(frm.age)) {
		alert("Age cannot be empty");
		valid = false;
	}
	else if (isEmpty(frm.mobile)) {
		alert("Mobile cannot be empty");
		valid = false;
	}
	else if (isEmpty(frm.email)) {
		alert("Email cannot be empty");
		valid = false;
	
	} 
	else if (isEmpty(frm.city)) {
		alert("city cannot be empty");
		valid = false;
	}

	else if (isNaN(frm.age.value)) {
		alert("Age should be a number");
		valid = false;
	}

	

	else if (isNaN(frm.mobile.value)) {
		alert("Mobile should be a number");
		valid = false;
	}

	
	return valid;

}

function isEmpty(obj) {
	if (obj.value == "")
		return true;
	else
		return false;
}
Posted
Updated 24-Aug-15 22:13pm
v3
Comments
Andy Lanng 25-Aug-15 3:58am    
Please don't code dump. It is considered impolite.
It is always best to add a quick sentence to tell us:
1: What you are trying to achieve
2: What the exact problem is with your code AND where in the code
3: *Tell us if you have debugged the code and what the results were

*Not everyone is aware that you can debug javascript in the browser, so this is easily forgiven
Member 11933436 25-Aug-15 4:07am    
we are submitting a registration form, where in the user has to input the details for that we are providing certain alerts.Now the question is instead of using many ifelse loops can we supress the code for checking the validations.(i think we can do using for loop) Would be happy if any anyone puts the code
Andy Lanng 25-Aug-15 4:16am    
Thanks ^_^
I updated the question for you. You can do this with the "Improve Question" button above these comments.
You should reply to peoples comments using the "reply" button. You have to hover over the comment to find it, though :S That way we get notified that you have responded.
You'll find you get a lot more responses following these steps.

I have posted a solution. Let me know if it suits your needs ^_^

1 solution

There is a way that is a bit of a hack. You can use switch(true):

JavaScript
var a = true;
var b = false;

switch(true){
    case a && b:
        alert("a && b");
        break;
    case b:
        alert("b");
        break;
    case b && !a:
        alert("b && !a");
        break;
    case a || b:
        alert("a || b");
        break;
    case a:
        alert("a");
        break;
    case a && !b:
        alert("a && !b");
        break;
    default:
        alert("default");
        break;
}


when a=false and b=false, the alert is "default".
when a=false and b=true, the alert is "b".
when a=true and b=false, the alert is "a || b".
When a=true and b=true, the alert is "a && b".

The break; is like the else here. You can omit the break; so it acts like a list of ifs instead of else ifs

Hope that helps ^_^
Andy

PS: you can play with this example here:
switch(true) on jsfiffle.net[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900