I want to perform client side validation of a simple form using jQuery, but I cannot use any plugins to validate. I want to display any error messages in a single warning window.
This is my form:
<form action="" method="post" class="a"> Name : <input type="text" class="text" name="name" id="name" /> <br/> Address : <input type="text" class="text" name="address" id="address" /> <br/> Email: <input type="text" class="text" name="email" id="email" /> <br/> <input type="button" id="submit" value="Submit" name="submit" /> </form>
If the user submits the form without filling in, I need to display 3 error messages in one warning window. Like this
Name can not be empty. Address can not be empty. email can not be empty.
If only one or two fields remain empty, I need to control the error message depending on the situation.
Here I tried this with jQuery. But warnings are displayed in separate fields:
My jQuery:
$('#submit').on('click', function() { valid = true; if ($('#name').val() == '') { alert ("please enter your name"); valid = false; } if ($('#address').val() == '') { alert ("please enter your address"); valid = false; } });
Here is the fiddle .
Can anyone tell me how I can figure this out? Thanks.
source share