Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  59] [ 2]  / answers: 1 / hits: 37346  / 11 Years ago, mon, july 29, 2013, 12:00:00

I know javascript in the beginning level, but I have a problem.



I have 7 input elements in a form and I want all of them to be filled.
I came up with this idea but it looks disgusting.



Can someone help me how to check whether all form elements are filled or not?



function validateForm()
{
var x=document.forms[register][name].value;
var y=document.forms[register][phone].value;
var z=document.forms[register][compname].value;
var q=document.forms[register][mail].value;
var w=document.forms[register][compphone].value;
var e=document.forms[register][adres].value;
var r=document.forms[register][zip].value;
if (x==null || x== || y==null || y== || z==null
|| z== || q==null || q== || w==null || w== || e==null || e==
|| r==null || r==)
{
alert(Please fill all the inputs);
return false;
}
}
</script>

More From » html

 Answers
8

This is the simple and dirty way.



A better way is to update a validation message that the fields are required.



function validateForm()
{
var fields = [name, phone, compname, mail, compphone, adres, zip]

var i, l = fields.length;
var fieldname;
for (i = 0; i < l; i++) {
fieldname = fields[i];
if (document.forms[register][fieldname].value === ) {
alert(fieldname + can not be empty);
return false;
}
}
return true;
}

[#76666] Saturday, July 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
frederickmohamedw

Total Points: 21
Total Questions: 123
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
frederickmohamedw questions
Wed, Sep 23, 20, 00:00, 4 Years ago
Sat, Jul 18, 20, 00:00, 4 Years ago
Sun, Apr 26, 20, 00:00, 4 Years ago
Sat, Jan 11, 20, 00:00, 4 Years ago
Fri, Dec 27, 19, 00:00, 4 Years ago
;