In this tutorial we will show you the solution of date validation in JavaScript using regular, here we defined one input field and submit button.
The input field collects user entered date and when user clicks on submit the onclick event calls dateValid() function.
In dateValid() function we checks user entered date with defined date pattern then respective message will alert users.
Step By Step Guide On Date Validation In JavaScript Using Regular Expression :-
Here we defined one input field and submit button with onclick event to declare function call ‘dateValid()’ function.
In dateValid() method we collects date input by id ‘date’ and stored to variable ‘v’.
The variable ‘ptrn’ used to store date pattern then using if condition we checked user entered input with defined pattern if both are same then 'date is correct format' message will displayed otherwise error message of 'Date is incorrect format and year input must start with 1900' will displayed.
<!DOCTYPE html> <html> <head> <title>DATE REX</title> </head> <body> <input type="text" id="date"> <input type="submit" value="Submit" onclick="dateValid()"> <script> function dateValid(){ var v=document.getElementById('date'); var ptrn=/^(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[-/.](19|20)\d\d$/; if(v.value.match(ptrn)){ alert('date is correct format'); } else{ alert('Date is incorrect format and year input must start with 1900'); } } </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here we defined one input field and submit button, this button defined with ‘onclick’ event to define dateValid() function call. So it initialize function when user clicks on ‘submit’ button.
- In script dateValid() function defined here variables ‘v,ptrn’ used to store user input date and date pattern then using if condition we checks user entered date input with pattern by match method.
- The match() method matches a string against a regular expression. It returns an array with the matches, if no match found it returns null.
- If both are matched it displays ‘date is correct format’ message on alertbox otherwise it displays ‘Date is incorrect format and year input must start with 1900’ message.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to validate date using regular expression in javascript.
When we executes our program on browser we can see one input field for user can enter any date and submit button when user clicks submit ‘onclick’ event will loads function dateValid() method.
It will checks whether user entered date is correct format or not and respective message will displays on alert box.
I hope this tutorial on date validation in JavaScript using regular expression helps you and the steps and method mentioned above are easy to follow and implement.