Validation function using javascript(onsubmit)

we are using onsubmit function for form validation. if we didn’t fill any required field it should notify the user that this field is empty. but we are getting same alert message for 2 times. if any one knows the solution please guide us

Post your code, we can’t do anything without code.

function validateForm() {
 var app = document.forms["myForm"]["applicantfullname"].value;
		 var phone = document.forms["myForm"]["phone"].value;
		  var email = document.forms["myForm"]["email"].value;
		  var add = document.forms["myForm"]["address1"].value;
		   var city = document.forms["myForm"]["City/Village"].value;
		    
 if (app == "" || phone == ""|| email== "" || add == "" || city == ""  ) {
            alert("please fill all feilds");
            return false;
        }
}
<form name="myForm" onSubmit="return validateForm()">
<input type="text" name="applicantfullname">
<input type="text" name="phone">
<input type="text" name="email">
<input type="text" name="address1">
<input type="text" name="City/Village">
<input type="submit">
</form>

Are you sure City/Village is a valid variable name?

I’m not too sure, but I’ve just pasted the code into Codepen and it seemed to work just fine.

What it does need though is labels on each field, and some kind of more granular validation on exactly what’s been entered. Right now, all fields will accept anything, including code.

1 Like

Or you could just use standard HTML5 and have a required attribute added to each input. No JS required at all!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.