Checking for empty fields using jQuery
Validation is an important technique in client-side scripting. Validation on the client side can significantly reduce round trips to the server by providing instant feedback in the form of messages. Even so, it is NOT recommended to rely on the client-side validation alone. JavaScript on the users' browsers might be turned off; therefore, validation should ALWAYS be done again on the server side as well.
How to do it...
Create a file for this recipe and name it
index.html
. Create a form with some text fields and aninput
button. Note that all textboxes exceptcity
has a class namerequired
assigned to them. This will be used while validating the fields.<html> <head> <title>Validate empty fields</title> <style type="text/css"> body{font-family:"Trebuchet MS",verdana;width:450px;} .error{ color:red; } #info{color:#008000;font-weight:bold; } </style> </head> <body> <...