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>
You could just do this:
I used this for my own form and it works fine while not taking up to much space or looking too "ugly". It works for all field elements and checks the value entered.
This is the simple and dirty way.
A better way is to update a validation message that the fields are required.
With some simple vanilla JS, you can handle this in a lot more simplified way:
JavaScript
Then make sure you
return
the function within your form, either inline (bad practice):Or in a more unobtrusive way: