Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article, I will explain that how to implement GST Number validation using Regular Expression in JavaScript.
Code for HTML Page
Implementation: On the HTML page I will place a I will place three controls one will be texbox with the id named text_gst_number to enter input by the user and other one will be lable of span with the id named lbl_error_gst to display error message and third one control will be button. After typing or entering the GST number user will click on the button with the id named submit_gst that will be validate the GST Number. On the onclick of the button i will call a function named check_GSTNumber()
<input type="text" id="text_gst_number" class="gst" />
<span id="lbl_error_gst" class="error"></span>
<br /><br />
<input type="button" id="submit_gst" value="Submit " onclick="check_GSTNumber()" />
Validating GST Number using Regular Expression in JavaScript
When the Submit Button will be clicked by the user, the check_GSTNumber JavaScript function will be executed.
Inside the check_GSTNumber JavaScript function, the GST Number TextBox is referenced and its value is tested based on a Regular Expression.
Below conditions need to be fullfiled.
It should be 15 characters long. As per the Government of India. The first 2 characters should be a digit representing State Code.The next 10 characters should be PAN number of the taxpayer or the user. The 13th character should be any digit (1-9) or an alphabet.The 14th character should be Z by default. The 15th character should be an alphabet or any digit. 0-9.
Example: 06BZAHM6385P6Z2
<script type="text/javascript">
function check_GSTNumber () {
var gstNumber = document.getElementById("text_gst_Number").value;
var lblError = document.getElementById("lbl_error_gst");
lblError.innerHTML = "";
var expr = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/;
if (!expr.test(gstNumber))
{
lblError.innerHTML = "Please enter valid GST Number.";
}
Else
{
lblError.innerHTML = "entered GST Number is valid.";
}
}
</script>
Below I am giving the complete code for this function. You can copy and paste the whole code on your HTML page to enjoy the execution of this program.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
.error { color: Red; }
.gst { text-transform: uppercase; }
</style>
</head>
<body>
GST Number:
<input type="text" id="text_gst_Number" class="gst" />
<span id="lbl_error_gst" class="error"></span>
<br /><br />
<input type="button" id="submit_gst" value="Submit" onclick="check_GSTNumber()"/>
<script type="text/javascript">
function check_GSTNumber() {
var gstNumber = document.getElementById("text_gst_Number").value;
var lblError = document.getElementById("lbl_error_gst");
lblError.innerHTML = "";
var expr = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/;
if (!expr.test(gstNumber))
{
lblError.innerHTML = "Please enter valid GST Number.";
}
else
{
lblError.innerHTML = "entered GST Number is valid.";
}
}
</script>
</body>
</html>
Conclusion: In above code, I have been explained about validation of GST number using JavaScript. This code is very helpful for every developer. Bye Bye and take care of you Developers. We will come back shortly with the new article.
Thanks and Regards
Using ASP.Net
No comments:
Post a Comment