Here in the first input field, we’re restricting the input to only lower case and upper case alphabets. To the second input field, we’re restricting it to only 2 digits from 0 to 9.
Note:^ symbol specifies the beginning and the $ symbol specifies the end of the input value.
You can share any simple/small to complex/big regular expression knowledge in the comment section below. Your time and effort is highly appreciated. Your knowledge and inputs will surely be helpful to many people around the world. Comment section is all yours :-)
If the username and the password fields are empty, we display appropriate message and skip the execution of $.post() method. This ensures that, we do not request data from the server when there is no need for it.
If the user has entered both username and password, then we execute $.post() method and pass the user entered data to process.php file.
At the beginning we check if the username or the password is empty. If they are empty, we echo Username and Password are madatory – from PHP and then stop further execution of the script.
If the username and password are not empty, then we check the user entered username against the usernames present inside the database. If the username is already present inside the database, then we intimate it to the user with a customized message.
Registration Form Validation: PHP + jQuery + AJAX (PART 2)
Why Validate both client side as well as server side ? What if javascript has been disabled on client machine i.e., the browser ? In this situation, our client side validation completely fails. So, server side validation is also important.
Then Why client side validation when server side validation could serve our purpose ? This is because, client side validation is faster. i.e., if user tries to register empty data, it needs to travel across, reach the server, execute the validation rules script and then travel back to report that the user had submitted empty data and is not acceptable! Instead of this lengthy, time consuming and costly process, we could simply write a client side validation and it responds back instantly, saving time, bandwidth and hence the cost of processing the data.