I want to do GST validation in javascript or jquery
Its limited to india only.
the structure of GST in India is as follows
GST Number will be of 15 digits having below validations:
a. First two positions will be numeric
b. Third to Sixth positions will be Alphabets
c. Seventh position will be either alphabet or numeric
d. Eighth to Eleventh positions will be numeric
e. Twelfth position will be an alphabet
f. Thirteenth to Fifteenth positions will be alphanumeric
I want to do GST validation in javascript or jquery
Its limited to india only.
the structure of GST in India is as follows
GST Number will be of 15 digits having below validations:
a. First two positions will be numeric
b. Third to Sixth positions will be Alphabets
c. Seventh position will be either alphabet or numeric
d. Eighth to Eleventh positions will be numeric
e. Twelfth position will be an alphabet
f. Thirteenth to Fifteenth positions will be alphanumeric
Share
Improve this question
asked Jun 15, 2017 at 15:02
ShreyaShreya
712 gold badges5 silver badges14 bronze badges
3
-
try to search js
substring
by using if – Donald Wu Commented Jun 15, 2017 at 15:03 -
AAABBBCCC123456
example GST... use substring and check is number or alphabet – Donald Wu Commented Jun 15, 2017 at 15:05 - Its very lengthy process,I need regular expression – Shreya Commented Jun 15, 2017 at 15:15
15 Answers
Reset to default 4([0-9]{2}[a-z]{4}([a-z]{1}|[0-9]{1}).[0-9]{3}[a-z]([a-z]|[0-9]){3})
Here is the regex for GST validation
http://regexr./ Test your GSTvalues with the above regex.
Here is the regex for GST validation in JavaScript:
var reggst = /^([0-9]){2}([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}([0-9]){1}([a-zA-Z]){1}([0-9]){1}?$/;
if(!reggst.test(gstinVal) && gstinVal!=''){
alert('GST Identification Number is not valid. It should be in this "11AAAAA1111Z1A1" format');
}
Finally i got GST validation regular expression with handle 15 digit only :)
/^([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}$/
$('#gst').on('change', function () {
var statecode = $(this).val().substring(0, 2);
var pancarno = $(this).val().substring(2, 12);
var entityNumber = $(this).val().substring(12, 13);
var defaultZvalue = $(this).val().substring(13, 14);
var checksumdigit = $(this).val().substring(14, 15);
if ($(this).val().length != 15) {
alert('GST Number is invalid');
$(this).focus();
return false;
}
if (pancarno.length != 10) {
alert('GST number is invalid ');
$(this).focus();
return false;
}
if (defaultZvalue !== 'Z') {
alert('GST Number is invalid Z not in Entered Gst Number');
$(this).focus();
}
if ($.isNumeric(statecode)) {
$('#gst_state_code').val(statecode).trigger('change');
} else {
alert('Please Enter Valid State Code');
$(this).focus();
}
if ($.isNumeric(checksumdigit)) {
return true;
} else {
alert('GST number is invalid last character must be digit');
$(this).focus();
}
});
Regex for GSTIN validation
/^([0-2][0-9]|[3][0-7])[A-Z]{3}[ABCFGHLJPTK][A-Z]\d{4}[A-Z][A-Z0-9][Z][A-Z0-9]$/
let vGST = (gnumber)=>{
let gstVal = gnumber;
let eMMessage = "No Errors";
let statecode = gstVal.substring(0, 2);
let patternstatecode=/^[0-9]{2}$/
let threetoseven = gstVal.substring(2, 7);
let patternthreetoseven=/^[A-Z]{5}$/
let seventoten = gstVal.substring(7, 11);
let patternseventoten=/^[0-9]{4}$/
let Twelveth = gstVal.substring(11, 12);
let patternTwelveth=/^[A-Z]{1}$/
let Thirteen = gstVal.substring(12, 13);
let patternThirteen=/^[1-9A-Z]{1}$/
let fourteen = gstVal.substring(13, 14);
let patternfourteen=/^Z$/
let fifteen = gstVal.substring(14, 15);
let patternfifteen=/^[0-9A-Z]{1}$/
if (gstVal.length != 15) {
eMMessage = 'Length should be restricted to 15 digits and should not allow anything more or less';
}else if (!patternstatecode.test(statecode)) {
eMMessage = 'First two characters of GSTIN should be numbers';
}else if (!patternthreetoseven.test(threetoseven)) {
eMMessage = 'Third to seventh characters of GSTIN should be alphabets';
}else if (!patternseventoten.test(seventoten)) {
eMMessage = 'Eighth to Eleventh characters of GSTIN should be numbers';
}else if (!patternTwelveth.test(Twelveth)) {
eMMessage = 'Twelveth character of GSTIN should be alphabet';
}else if (!patternThirteen.test(Thirteen)) {
eMMessage = 'Thirteen characters of GSTIN can be either alphabet or numeric';
}else if (!patternfourteen.test(fourteen)) {
eMMessage = 'fourteen characters of GSTIN should be Z';
}else if (!patternfifteen.test(fifteen)) {
eMMessage = 'fifteen characters of GSTIN can be either alphabet or numeric';
}
console.log(eMMessage)
}
vGST("33");
vGST("12ABCDE1234K11S");
vGST("12ABCDE1234K1ZS");
var reggst = /^([0-9]){2}([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}([0-9]){1}([a-zA-Z]){1}([0-9]){1}?$/;
if(!reggst.test(gstinVal) && gstinVal!='' && gstinval.length!=15){
alert('GST Identification Number is not valid. It should be in this "11AAAAA1111Z1A1" format');
}
You can simply get the length of the field value and pare it with the count that you want.
You can do that with a regular expression as follows
var regex = /^\d{2}\w{4}[a-zA-Z0-9]{1}\d{3}\w{1}[a-zA-Z0-9]{3}$/i;
if (regex.test('GSTNumber')) { console.log('valid'); }
Here is the regex for GST Number validation which validate for 15 alphanumeric characters :
var regex ="\d{2}[A-Z]{5}\d{4}[A-Z]{1}\d{1}[A-Z]{1}\d{1}"
Here is a regex code that i am currently using on my project for gst number validation.
^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9]{1}Z[a-zA-Z0-9]{1}$
thank you.
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).on('change',".gstinnumber", function(){
var inputvalues = $(this).val();
var gstinformat = new RegExp('^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$');
if (gstinformat.test(inputvalues)) {
return true;
} else {
alert('Please Enter Valid GSTIN Number');
$(".gstinnumber").val('');
$(".gstinnumber").focus();
}
});
</script>
I use this functions in my code
function check_gst(gst){
if(gst.length != 15){
alert("Invalid Length of GSTIN");
return false;
}else{
var state = parseInt(gst.substring(0, 2));
// FIRST 2 CHARACTERS STATE CODE
if(state < 1 || state > 37){
alert("Invalid First Two Characters of GSTIN");
return false;
}
// NEXT 10 CHARACTERS PAN NO. VALIDATION
var pan = gst.substring(2, 12).toUpperCase();
var regex = /[a-zA-Z]{3}[PCHFATBLJG]{1}[a-zA-Z]{1}[0-9]{4}[a-zA-Z]{1}$/;
if( !regex.test(pan) ){
alert("Invalid GSTIN");
return false;
}
// DEFAULT 14TH CHARACTER 'Z'
var char14 = gst[13].toUpperCase();
if(char14 != "Z"){
alert("14th character of GSTIN should be 'Z'");
return false;
}
// CHECKSUM DIGIT
if(check_gst_checksum(gst.substring(0, 14)) != gst[14]){
alert("Invalid GSTIN");
return false;
}
return true;
}
}
String.prototype.getw = function( value ) {
for( var prop in this ) {
if( this.hasOwnProperty( prop ) ) {
if( this[ prop ] === value )
return prop;
}
}
}
function check_gst_checksum(gst_wo){
weight_c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
gst = gst_wo.toUpperCase();
var total_a1 = 0;
for (var i = 0; i < gst.length; i++) {
var weight = weight_c.getw(gst[i]);
var factor = ( (i % 2) +1 );
var product = weight * factor;
var qu = Math.floor( product/36 );
var re = product % 36;
var a1 = qu + re;
total_a1 += a1;
}
var d = total_a1 % 36;
var dd = 36 - d;
return weight_c[dd];
}
All the calculation for checksum digit is given at this blog https://gauravj75.blogspot.in/2018/01/excel-gstin-checksum-digit-logic.html
It's too late to respond but someone will help this.
First two Digits-The first digit of GSTIN is state code as per Indian Census 2011. The state codes are as below.
01-Jammu and Kashmir, 02-Himachal Pradesh, 03-Punjab, 04-Chandigarh, 05-Uttarakhand, 06-Haryana, 07-Delhi, 08-Rajastan, 09-UP, 10-Bihar, 11-Sikkim, 12-Arunachal Pradesh, 13-Nagaland, 14-Manipur, 15-Mizoram, 16-Tripura, 17-Meghalaya, 18-Assam, 19-West Bengal, 20-Jharkhand, 21-Orrissa, 22-Chattisgarh, 23-MP, 24-Gujarat, 25-Daman and Diu, 26-Dadar and Nagar Haveli, 27-Maharashtra, 28-Andhra Pradesh, 29-Karnataka, 30-Goa, 31-Lakshadweep, 32-Kerala, 33-Tamil Nadu, 34-Puducherry and 35-Anadaman and Nicobar Islands.
Next 10 Digits-It is the PAN number of a business entity like your shop, mall or pany.
13th Digit-It indicates the number of registrations as a business entity has within a state for the same PAN. It will be an alpha-numeric number (first 1-9 and then A-Z) and will be assigned on the basis of the number of registrations a legal entity (having the same PAN) has within one state.
Let say the pany ABC registered in the same state for 5 times for different businesses. In such case, this digit will be printed as 5. Let us assume, the same pany registered for around 15 times, then it should be represented as F (1-9 numeric and later on 10th registration be named as A and 11th as B and so on up to Z).
Hence, a business entity can register the GSTIN within a single state for the maximum of 35 times (1-9 and later on A-Z).
14th Digit-It will be by default as Z.
15th Digit-The last digit will be a check code which will be used for detection of errors.
With this information working RegEX is below
^[0-9]{2}[A-Za-z]{3}[CPHFATBLJGcphfatblj]{1}[A-Za-z]{1}[0-9]{4}[A-Za-z]{1}[0-9A-Za-z]{1}(Z|z)[0-9A-Za-z]{1}$
This is perfect GST Validation code.
This will definitely work with your jQuery.
rules: {
gst_number_value: {
required: true,
minlength: 15,
maxlength: 15,
pattern: '^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9]{1}[A-Z]{2}$',
},
},
messages: {
required: "Please enter GST number",
minlength: "Please check your GST number.",
maxlength: "Please check your GST number.",
pattern: "You have entered an invalid GST number.",
},
$("#txtGSTN").change(function () {
var inputvalues = $(this).val();
var gstinformat = new RegExp(
"^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]1}[1-9A-Z]{1}Z[0-9A-Z]{1}$"
);
if (gstinformat.test(inputvalues)) {
return true;
} else {
alert("Please Enter Valid GSTIN Number");
$("#txtGSTN").val("");
$("#txtGSTN").focus();
}
});