# Get pattern from string in jQuery

**URL:** <https://www.phphelp.com/t/get-pattern-from-string-in-jquery/33480>\
**Category:** Javascript & Ajax\
**Created:** [May 11, 2021, 5:59am UTC](https://www.phphelp.com/t/get-pattern-from-string-in-jquery/33480 "2021-05-11T05:59:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![devac9039](https://www.phphelp.com/user_avatar/www.phphelp.com/devac9039/32/3647_2.png) [@devac9039](https://www.phphelp.com/u/devac9039)\
**Post date:** [May 11, 2021, 5:59am UTC](https://www.phphelp.com/t/get-pattern-from-string-in-jquery/33480/1 "2021-05-11T05:59:39Z")

</div>

I want to create the function, If we input the correct GSTIN format, it will auto find the pattern from GSTIN and set it in PAN textbox as its value automatically.  
basically, PAN ID is always in GSTIN ID, below the image which represents where the PAN number show in GSTIN.  
As I `keyup` in jQuery the code will automatically fetch the PAN from GSTIN and show in `input#pan`  
Please help me, I am new in jQuery and Js.

```auto
PAN PATTERN IS: /^[A-Za-z]{3}[JjLlBbTtAaFfHhPpGg]{1}[A-Za-z]{1}[0-9]{4}[A-Za-z]{1}$/;
GSTIN PATTREN IS: /^([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}$/

```

```auto
My PAN is: AAAAA0000A
My GSTIN is: 22AAAAA0000A1Z5

```

 ![](https://www.phphelp.com/uploads/default/original/2X/3/311c3593fca9b5759c748d1cf01915f6993c61f1.png)

```auto
<div class="form-group">
  <label>gstin</label>
  <div class="input-group">
  <input required="" type="text" class="form-control" name="gstin" id="gstin" placeholder="gstin" value=""/>
  </div>
</div>

<div class="form-group">
	<label>PAN</label>
	<div class="input-group">
  <input maxlength="10" required="" type="text" class="form-control" name="pan" id="pan" placeholder="PAN ID" value="" title="PAN Number must be 10 character" readonly/>
	</div>
</div>

```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [May 12, 2021, 6:50pm UTC](https://www.phphelp.com/t/get-pattern-from-string-in-jquery/33480/2 "2021-05-12T18:50:26Z")

</div>

Just use SLICE… `.slice(start, end)`

If you have a value create something like this.slice(0,1) to get the state code or this.slice(2,10) to get pan…

---

<div class="post-metadata">

**Author:** ![devac9039](https://www.phphelp.com/user_avatar/www.phphelp.com/devac9039/32/3647_2.png) [@devac9039](https://www.phphelp.com/u/devac9039)\
**Post date:** [May 15, 2021, 5:43pm UTC](https://www.phphelp.com/t/get-pattern-from-string-in-jquery/33480/3 "2021-05-15T17:43:29Z")

</div>

could you please show me this in script. I really never use the function you are saying… even also I new in jQuery. Please help , If possible.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [May 16, 2021, 4:30am UTC](https://www.phphelp.com/t/get-pattern-from-string-in-jquery/33480/4 "2021-05-16T04:30:08Z")

</div>

Well, I just got back as saw your post. Basically to do this in Jquery, you would need a few steps.  
First, you would need to have JQuery monitor the input field. Then, you would need to use slice function  
to get the parts needed. Lastly, you would need it to store them in the other fields. But, I did some research for you and you can not use slice functions. You need to use substr functions. Something like this might work. ( Full test page. Load it to the server and test and then study the code. )

```auto
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function(){
    var $gstin = $('#gstin');
    var $static = $('#static');
	var $pan = $('#pan');
    function onChange() {
        $static.val($gstin.val().substr(0,2));
		$pan.val($gstin.val().substr(12,3));
    };
    $('#gstin')
        .change(onChange)
        .keyup(onChange);
});
</script>
<input type="text" name="gstin" id="gstin" ><br><br>
<input type="text" name="static" id="static"><br><br>
<input type="text" name="pan" id="pan">

</body>
</html>

```
