Javascript GST 标识号 (GSTIN) 的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44431819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex for GST Identification Number (GSTIN)
提问by Minkesh Jain
What is the regex for the GST number in India. You can read more about the GST numbers from https://cleartax.in/s/know-your-gstinOn a summary level the number is represented as
印度 GST 编号的正则表达式是什么。您可以从https://cleartax.in/s/know-your-gstin阅读有关 GST 数字的更多信息 在 摘要级别,该数字表示为
- List itemThe first two digits of this number will represent the state code as per Indian Census 2011
- 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 check code
- 列表项目此数字的前两位数字将代表 2011 年印度人口普查中的州代码
- 接下来的十位数字将是纳税人的 PAN 号码
- 第十三位将根据一个州内的注册数量分配
- 第十四位默认为 Z
- 最后一位是校验码
回答by tk120404
Here is the regex and checksum validation for GSTIN
这是 GSTIN 的正则表达式和校验和验证
\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}
Format details
格式详情
- First 2 digits of the GST Number will represent State Code as per the Census (2011).
- Next 10 digits will be same as in the PAN number of the taxpayer.
- First five will be alphabets
- Next four will be numbers
- Last will be check code
- The 13th digit will be the number of registration you take within a state i.e. after 9, A to Z is considered as 10 to 35 .
- 14th digit will be Z by default.
- Last would be the check code.
- 根据人口普查(2011),GST 编号的前 2 位数字将代表州代码。
- 接下来的 10 位数字将与纳税人的 PAN 号码相同。
- 前五个将是字母
- 接下来的四个将是数字
- 最后将是检查代码
- 第 13 位数字将是您在一个州内注册的数量,即在 9 之后,A 到 Z 被视为 10 到 35。
- 第 14 位数字默认为 Z。
- 最后是校验码。
Here is the code for verifying/validating the gstin number using the checksum in js
这是使用js中的校验和验证/验证gstin编号的代码
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'))
回答by Mitchell Anicas
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].
根据H&R Block India GSTIN 指南,第 13 个“数字”(实体代码)是“一个字母数字数字(第一个 1-9,然后是 AZ)”。也就是说,不允许为零,AZ 代表 10-35。因此[1-9A-Z]比 更准确[0-9]。
The last digit, "check digit", is indeed alphanumeric: [0-9A-Z]. I have independently confirmed by obtaining and testing actual GSTINs.
最后一位数字“校验位”确实是字母数字:[0-9A-Z]。我已通过获取和测试实际 GSTIN 独立确认。
回答by jeff-thexman
The correct validation for GSTIN should be
GSTIN 的正确验证应该是
^([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.
前 2 位数字表示州代码 (01-37),如土地区域代码列表中所定义。
The next 10 characters pertain to PAN Number in AAAAA9999X format.
接下来的 10 个字符与 AAAAA9999X 格式的 PAN 编号有关。
13th character indicates the number of registrations an entity has within a state for the same PAN.
第 13 个字符表示实体在同一 PAN 的状态内的注册数量。
14th character is currently defaulted to "Z"
第 14 个字符当前默认为“Z”
15th character is a checksum digit
第 15 个字符是校验和数字
This regex pattern accommodates lower and upper case.
此正则表达式模式适用于小写和大写。
回答by Viral Jain
To add to above answers, this answer also provides code snippet for 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;
}
}
Source: GST Google Group Link, Code Snippet Link
来源:GST 谷歌群组链接, 代码片段链接
回答by N?s????? ?
Try this one with jQuery
用 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>
回答by Atishay
The correct regexfor GSTINis as under-
正确regex的GSTIN如下-
^([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})+$
The above one is correct which have been applied to more than 300 valid taxpayers whom you can validate from this link
以上是正确的,已应用于300多个有效纳税人,您可以从此链接验证
回答by Gaurav Saxena
I Used This One And Checked It Against 30+ GSTINand it worked flawless.
我使用了这个并根据30+ GSTIN检查了它,它完美无缺。
/^[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 GSTIN i came across.
在我遇到的一些 GSTIN 中,最后一个校验位似乎也是字母数字。
回答by Dinesh Yadav
Try this. It is working as per GSTIN.
尝试这个。它按照 GSTIN 工作。
^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$
回答by Minkesh Jain
Regex should be:
正则表达式应该是:
/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[0-9]{1}Z[0-9]{1}/
回答by Sarafraz khan
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.
这个对我有用。


