Aadhaar Number validation using Javascript
Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article I will explain about how to implement Aadhaar Number validation using Regular Expression (Regex) in Javascript.
Code for HTML Page
On the HTML page i will place a textbox to enter aadhar number by te user with the id named txt_Aadhaar. i will also place a span for display the entered aadhar number entered by the user. i will also place a Button with the id named btn_Submit and here i assigned a Javascript OnClick event handler to the button.
Please Enter your Aadhaar Number:
<input type="text" id="txt_Aadhaar" />
<span id="lbl_Error" class="error"></span>
<input type="button" id="btn_Submit" value="Submit" onclick="ValidateAadhaar()"/>
Validating Aadhaar Number using Regular Expression in JavaScript
When user press the enter button after entering the aadhar number the function
named Validation_Aadhaar Javascript function will be called.
Inside the ValidateAadhaar Javascript function, the Aadhaar Number TextBox is referenced and its value is tested using a Regular expression.
Below mentioned Aadhaar Number formats will work as valid.
1. 12 digits without space. Ex: 123456789012
2. White space after every 4th digit. Ex: 1234 5678 9012
3. Hyphen (-) after every 4th digit. Ex: 1234-5678-9012
Finally, if the Aadhaar Number format is incorrect, then an HTML SPAN element with an error message is displayed.
<script type="text/javascript">
function Validation_Aadhaar() {
var aadhaar = document.getElementById("txt_Aadhaar").value;
var lbl_Error = document.getElementById("lbl_Error");
lbl_Error.innerHTML = "";
var expr = /^([0-9]{4}[0-9]{4}[0-9]{4}$)|([0-9]{4}\s[0-9]{4}\s[0-9]{4}$)|([0-9]{4}-[0-9]{4}-[0-9]{4}$)/;
if (!expr.test(aadhaar)) {
lbl_Error.innerHTML = "Invalid Aadhaar Number";
}
}
</script>
Here i am giving complete code for HTML page. You can copy and paste the whole code on your HTML page for enjoy the exaction of the code.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body { font-family: Arial; font-size: 15pt; }
.error { color: Red; }
</style>
</head>
<body>
Please Enter your Aadhaar Number:
<input type="text" id="txt_Aadhaar" />
<span id="lbl_Error" class="error"></span>
<br/><br/>
<input type="button" id="btn_Submit" value="Submit" onclick="ValidateAadhaar()"/>
<script type="text/javascript">
function Validation_Aadhaar() {
var aadhaar = document.getElementById("txt_Aadhaar").value;
var lbl_Error = document.getElementById("lbl_Error");
lbl_Error.innerHTML = "";
var expr = /^([0-9]{4}[0-9]{4}[0-9]{4}$)|([0-9]{4}\s[0-9]{4}\s[0-9]{4}$)|([0-9]{4}-[0-9]{4}-[0-9]{4}$)/;
if (!expr.test(aadhaar)) {
lbl_Error.innerHTML = "Invalid Aadhaar Number";
}
}
</script>
</body>
</html>
Conclusion: In above code, I explained that how to implement Aadhaar Number validation using Regular Expression (Regex) in Javascript. This code is very helpful for every developer. Bye Bye and take care of yourself Developers. We will come back shortly with the new article.
Regards
Using Asp.net
Comments
Post a Comment