Regular expression for GST Identification Number (GSTIN) - javascript

What is the regex for the GST number in India?
You can read more about the GST numbers in What is GST Number? – Know your 15 Digits GSTIN. On a summary level, the number is represented as
List item. The first two digits of this number will represent the state code as per 2011 Census of India
The next ten digits will be the PAN number of the taxpayer
The thirteenth digit will be assigned based on the number of registration within a state
The fourteenth digit will be Z by default
The last digit will be for a check code

Here is the regex and checksum validation for GSTIN:
\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}
Format details
The first two digits of the GST Number will represent the State Code as per the Census (2011).
The next 10 digits will be same as in the PAN number of the taxpayer.
The first five will be alphabets
The next four will be numbers
The last will be the check code
The 13th digit will be the number of registrations you take within a state, i.e., after 9, A to Z are considered as 10 to 35.
The 14th digit will be Z by default.
The last would be the check code.
Here is the code for verifying/validating the GSTIN number using the checksum in JavaScript
function checksum(g){
let regTest = /\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}/.test(g)
if(regTest){
let a=65,b=55,c=36;
return Array['from'](g).reduce((i,j,k,g)=>{
p=(p=(j.charCodeAt(0)<a?parseInt(j):j.charCodeAt(0)-b)*(k%2+1))>c?1+(p-c):p;
return k<14?i+p:j==((c=(c-(i%c)))<10?c:String.fromCharCode(c+b));
},0);
}
return regTest
}
console.log(checksum('27AAPFU0939F1ZV'))
console.log(checksum('27AASCS2460H1Z0'))
console.log(checksum('29AAGCB7383J1Z4'))
GST regex and checksum in various programming languages

Here is the regex that I came up with:
/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/
According to H&R Block India GSTIN guide, the 13th 'digit' (entity code) is "an alpha-numeric number (first 1-9 and then A-Z)". That is, zero is not allowed and A-Z represent 10-35. Hence the [1-9A-Z] is more accurate than [0-9].
The last digit, "check digit", is indeed alphanumeric: [0-9A-Z]. I have independently confirmed by obtaining and testing actual GSTINs.

The correct validation for GSTIN should be
^([0][1-9]|[1-2][0-9]|[3][0-7])([a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9a-zA-Z]{1}[zZ]{1}[0-9a-zA-Z]{1})+$
The first 2 digits denote the State Code (01-37) as defined in the Code List for Land Regions.
The next 10 characters pertain to PAN Number in AAAAA9999X format.
13th character indicates the number of registrations an entity has within a state for the same PAN.
14th character is currently defaulted to "Z"
15th character is a checksum digit
This regex pattern accommodates lower and upper case.

To add to the previous answers, this answer also provides a code snippet for the checksum digit.
public static final String GSTINFORMAT_REGEX = "[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9A-Za-z]{1}[Z]{1}[0-9a-zA-Z]{1}";
public static final String GSTN_CODEPOINT_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String getGSTINWithCheckDigit(String gstinWOCheckDigit) throws Exception {
int factor = 2;
int sum = 0;
int checkCodePoint = 0;
char[] cpChars;
char[] inputChars;
try {
if (gstinWOCheckDigit == null) {
throw new Exception("GSTIN supplied for checkdigit calculation is null");
}
cpChars = GSTN_CODEPOINT_CHARS.toCharArray();
inputChars = gstinWOCheckDigit.trim().toUpperCase().toCharArray();
int mod = cpChars.length;
for (int i = inputChars.length - 1; i >= 0; i--) {
int codePoint = -1;
for (int j = 0; j < cpChars.length; j++) {
if (cpChars[j] == inputChars[i]) {
codePoint = j;
}
}
int digit = factor * codePoint;
factor = (factor == 2) ? 1 : 2;
digit = (digit / mod) + (digit % mod);
sum += digit;
}
checkCodePoint = (mod - (sum % mod)) % mod;
return gstinWOCheckDigit + cpChars[checkCodePoint];
} finally {
inputChars = null;
cpChars = null;
}
}
Sources:
GST Google Group Link,
Code Snippet Link

This is a 100% accurate regex of GSTIN, as it checks everything mentioned in the above image.
[0-9]{2}[A-Z]{3}[ABCFGHLJPTF]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}

Try this one with jQuery
$(document).ready(function() {
$.validator.addMethod("gst", function(value3, element3) {
var gst_value = value3.toUpperCase();
var reg = /^([0-9]{2}[a-zA-Z]{4}([a-zA-Z]{1}|[0-9]{1})[0-9]{4}[a-zA-Z]{1}([a-zA-Z]|[0-9]){3}){0,15}$/;
if (this.optional(element3)) {
return true;
}
if (gst_value.match(reg)) {
return true;
} else {
return false;
}
}, "Please specify a valid GSTTIN Number");
$('#myform').validate({ // initialize the plugin
rules: {
gst: {
required: true,
gst: true
}
},
submitHandler: function(form) {
alert('valid form submitted');
return false;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>
<form id="myform" action="" method="post">
<div>
<label>GSTTIN #</label>
<div>
<input type="text" name="gst" value="" id="input-gst" />
</div>
</div>
<button type="submit">Register</button>
</form>

I used this one and checked it against 30+ GSTINs, and it worked flawlessly.
/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/
The last check digit also seems to be alphanumeric in some of the GSTINs I came across.

Try this.
It is working as per GSTIN.
^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$

The correct regex for GSTIN is as:
^([0][1-9]|[1-2][0-9]|[3][0-7])([A-Z]{5})([0-9]{4})([A-Z]{1}[1-9A-Z]{1})([Z]{1})([0-9A-Z]{1})+$
It is correct and have been applied to more than 300 valid taxpayers whom you can validate from this link.

The regex should be:
/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[0-9]{1}Z[0-9]{1}$/

The correct regex could be:
/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[0-9]{1}Z[0-9]{1}?$/
It works for me.

My working regex is
/^([0][1-9]|[1-2][0-9]|[3][0-8])[A-Z]{3}[ABCFGHLJPTF]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}/
According to 38 States as of the year 2021.

Correct GSTIN validation will be covering 27 states of India,
^([0][1-9]|[1-2][0-9]|[3][0-7])([a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9a-zA-Z]{1}[zZ]{1}[0-9a-zA-Z]{1})+$

If you want a Node.js library, you can use GSTIN Validator:
var validator = require('gstin-validator');
validator.isValidGSTNumber('12AAACI1681G1Z0'); // Returns a Boolean value.
validator.ValidateGSTIN('47AAACI1681G1Z0'); // Returns a response string with an error message
validator.getGSTINInfo('12AAACI1681G1Z0'); // Returns metadata for GSTIN based on the followed numbering scheme.

Related

Regular expression to validate National Identity Card number which has 9 digits with final digit being X or V

function PassportValidate(s, e) {
var option = rbIdentityVerification.GetValue().toString(); //gets the selected option which is NIC selected by default. devexpress radiobuttonlist
var text = e.value.toString(); //gets the value from the textbox
if (option == "NIC") {
var pattern2 = RegExp("^\d{9}(X|V)$");
if (!pattern2.test(text)) {
e.isValid = false;
e.errorText = "Passport number invalid.(Max 9 numbers)"; //Error message that is to be shown
}
}
}
Regular expression to validate National Identity Card number which has 9 digits with final digit being X or V. the code always return invalid, so even a correct NIC is entered still returns as invalid
Though \d is modern regex shorthand for [0-9], I prefer to use latter.
This regex work fine ^([0-9]{9})(X|V)$.
Here is the working jsfiddle.
Try using This (Sri Lanka NIC new and old formats). This will accept all the numbers with V or X (Upper or lower).
/^([0-9]{9})(X|V)$|^([0-9]{11})/gis
I think this may will help you
^\d{9}[V|v|X|x]$

regex only number, dash is optional

I'm trying to make a javascript function with a regex, that will validate a phone number.
the rules are :
1. numbers only.
2. more then 10 numbers.
3. a dash ( - ) is allowed (optional).
first, I tried this one :
function validatePhone(phone) {
var phoneReg = /[0-9]{10,}/;
return (phoneReg.test(phone));
}
it worked well only on the first 2 rules, but not with the dash.
Then I tried var phoneReg = /[-0-9]{10,}/; and even var phoneReg = [\d]+\-?[\d]+ but then the javascript was broken...
any thoughts ?
This is how I would approach phone number validation:
var validatePhone = function(phone) {
// Stip everything but the digits.
// People like to format phone numbers in all
// sorts of ways so we shouldn't complain
// about any of the formatting, just ensure the
// right number of digits exist.
phone = phone.replace(/\D/g, '');
// They should have entered 10-14 digits.
// 10 digits would be sans-country code,
// 14 would be the longest possible country code of 4 digits.
// Return `false` if the digit range isn't met.
if (!phone.match(/\d{10,14}/)) return false;
// If they entered 10, they have left out the country code.
// For this example we'll assume the US code of '1'.
if (phone.length === 10) phone = '1' + phone;
// This is a valid number, return the stripped number
// for reformatting and/or database storage.
return phone;
}
This should work. The - character needs to be escaped.
var phoneReg = /[0-9-\-]{11,}/;
The potential problem with this, is that strings that have multiple dashes will test positive even when 10 numbers aren't in the string. I would suggest replacing dashes before testing.
var phoneReg = /[0-9]{11,}/;
return (phoneReg.test(phone.replace(/\-/g, ''));

JQuery Phone Number Validation

I've am using jQuery validation plugin to validate a mobile phone number and am 2/3 of the way there.
The number must:
Not be blank - Done,
Be exactly 11 digits - Done,
Begin with '07' - HELP!!
The required rule pretty much took care of itself and and I managed to find the field length as a custom method that someone had shared on another site.
Here is the custom field length code. Could anyone please suggest what code to add where to also require it begin with '07'?
$.validator.addMethod("phone", function(phone_number, element) {
var digits = "0123456789";
var phoneNumberDelimiters = "()- ext.";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 11;
s=stripCharsInBag(phone_number,validWorldPhoneChars);
return this.optional(element) || isInteger(s) && s.length >= minDigitsInIPhoneNumber;
}, "* Your phone number must be 11 digits");
function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
$(document).ready(function(){
$("#form").validate();
});
The code in the question seems a very complicated way to work this out. You can check the length, the prefix and that all characters are digits with a single regex:
if (!/^07\d{9}$/.test(num)) {
// "Invalid phone number: must have exactly 11 digits and begin with "07";
}
Explanation of /^07\d{9}$/ - beginning of string followed by "07" followed by exactly 9 digits followed by end of string.
If you wanted to put it in a function:
function isValidPhoneNumber(num) {
return /^07\d{9}$/.test(num);
}
If in future you don't want to test for the prefix you can test just for numeric digits and length with:
/^\d{11}$/
You could use this function:
function checkFirstDigits(s, check){
if(s.substring(0,check.length)==check) return true;
return false;
}
s would be the string, and check would be what you are checking against (i.e. '07').
Thanks for all the answers. I've managed to come up with this using nnnnnn's regular expression. It gives the custom error message when an incorrect value is entered and has reduced 35 lines of code to 6!
$.validator.addMethod("phone", function(phone_number, element) {
return this.optional(element) || /^07\d{9}$/.test(phone_number);
}, "* Must be 11 digits and begin with 07");
$(document).ready(function(){
$("#form").validate();
});
Extra thanks to nnnnnn for the regex! :D
Use indexOf():
if (digits.indexOf('07') != 0){
// the digits string, presumably the number, didn't start with '07'
}
Reference:
indexOf().

Novice Javascript query about bad input

I am making a simple tip calculator to help myself learn Javascript. The problem I can't solve is how to compensate for "bad input".
In the code below if the user prefaces the numeric input amount with a dollar sign $, the result is NAN.
function tipAmount(){
var dinner=prompt("How much was dinner?");
result = dinner*.10;
alert("Your tip is " +"$"+result );
}
How do I fix that.
You can try to parse out the numeric value with a regular expression:
var match = dinner.match(/\d+\.?\d*/); // parse with a regular expression
if(!match) { // not able to parse
alert("wrong");
}
var price = +match[0]; // convert to a number
result = price * .10;
The regular expression /\d+\.?\d*/ means: one or more digits, and possibly a dot with other digits following. This means that if e.g. dinner is "$1.23", price will be the number 1.23. The same goes for "$ 1.23" or "1.23 dollar" etc - the number will be parsed out with the pattern defined by the regular expression.
The simplest way would be to parse the input into a float, and see if NaN is returned.
if (isNaN(parseFloat(dinner)))
alert("Bad Input")
Just note that 45.2WWW will return 45.2, and so the above will pass.
If you want to make sure what the user typed in is exactly a number, you could do something like this:
var str = '3.445';
var num = parseFloat(str);
if (isNaN(num) || str.length !== num.toString().length)
alert("Bad Input");
try to parse the input as float or integer depending on your needs:
var dinner = parseFloat(prompt("How much was dinner?"));
or
var dinner = parseInt(prompt("How much was dinner?"));
this functions return 0 whether they unable to parse the input as number
Given your approach of using alerts, the following will work:
function tipAmount() {
var dinner=prompt("How much was dinner?");
//convert "dinner" to a number, stripping out any non numeric data
dinner = Number(dinner.replace(/[^0-9\.]+/g,""));
//any unknown data will convert to 0
if(dinner <= 0) {
alert("Please enter a valid amount");
return false;
}
var result = dinner*.10;
alert("Your tip is " +"$"+result );
return true;
}
Please tip more!
Just check if the value is numeric - Javascript's isNaN:
if (isNaN(dinner)) {
alert('Bad number, bub.');
return;
}
Or, if you want to allow users to type in both - just number or an amount with $ at the beginning, you can check for first char:
if( dinner.charAt(0) == '$' )
{
dinner = dinner.substring(1);
}
This way, whenever user types $, your app will just remove it. If they type a normal number it will calculate the tip for you...

How to store more than 10 digit number in javascript using the var?

First of all,
What am i doing ?
I have to set the limit of emails in our product in webpage.It's handled with the javascript for validation.It handles upto 8 digit numbers fine. But in our QA team enters the more than 17 digit number in the text box of other email field.It throw the negative message.What can i do ???
My sample code is:
if(form.otherEmails) {
if(validEmailArray.endsWith(',')){
var otherEmailLength = validEmailArray.substring(0,validEmailArray.length-1).split(",");
var setLimitOtherEmail = window.parent.document.getElementById('setLimitOtherEmail').value;
if(setLimitOtherEmail == '-1'){
form.otherEmails.value = otherEmailLength;
}
else if(otherEmailLength.length <= setLimitOtherEmail){
form.otherEmails.value = otherEmailLength;
}
else{
alert("More than "+setLimitOtherEmail+ " " +"Recipient emailIds not allowed in this section.\nIf you want to send it to more recipients, Please create a Bulk Contact Group.");
form.otherEmails.focus();
return false;
}
}
else
form.otherEmails.value = validEmailArray;
}
This is due to the limit being a string, and when a string is being compared to a number (length) the number is coerced into a string, not the other way around.
These are then compared lexicographically - and lexicographically "9" is more (>) than "19".
You need to use parseInt(setLimitOtherEmail, 10) to get the value as a number before comparing them.
Try parsing each of the numbers into Integers before performing any comparison operations on them.
var setLimitOtherEmail = parseInt(window.parent.document.getElementById('setLimitOtherEmail').value);
Other than that are you certain otherEmailLength is actually the number that you want? From the looks of it you are taking the substring of validEmail array and splitting it on "," but it doesn't look like you actually get the length of the array. Try adding .length to the end of the value of otherEmailLength.
var otherEmailLength = validEmailArray.substring(0,validEmailArray.length-1).split(",").length;

Categories