Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to validate a form in jquery without using jQuery plugin Validate?
Posted
Updated 11-Jun-18 23:58pm

 
Share this answer
 
v2
Loop through each input element in the form, and check if it has a value. If not append the error message to a string which you later alert out if valid is false.

$('#submit').on('click', function() {
var valid = true,
message = '';

$('form input').each(function() {
var $this = $(this);

if(!$this.val()) {
var inputName = $this.attr('name');
valid = false;
message += 'Please enter your ' + inputName + '\n';
}
});

if(!valid) {
alert(message);
}
});
 
Share this answer
 
Comments
Richard Deeming 12-Jun-18 10:40am    
Asked and answered THREE YEARS AGO.

Stick to answering recent questions.

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