• Resolved Rohit Katre

    (@rohitatwp)


    Dear Friends,

    I am using RegExp at my ppix.tv site on Form 10. I managed to add two of my own custom expressions viz. the Indian PAN No. & the Indian Aadhar No. The entered data gets accommodated fine with what I have created. However, I wish it to be a bit smart by way of getting automatically formatted. And therein I need help.

    The PAN No. is a 10 digit alphanumeric. For example let us take mine. It is AAKPK4764H. My expression allows it to be stored either with spaces like AAKPK 4764 H or with hyphens like AAKPK-4764-H. But whereas if the user is to not put in either the space or the hyphen and just enter the 10 digits then I want the plugin to by default add the hyphen in a 5-4-1 manner in the end result. I also want the user’s lower case entry converted to an upper case entry e.g. aakpk 4764 h should automatically get converted to AAKPK-4764-H. Can you kindly help me achieve this?

    Also, the same way for the Aadhar No. I wish that the plugin by default make it 4-4-4 by automatically adding the hyphens after every 4 digits if the user has not added them manually. Can someone kindly help me achieve this?

    My PAN Regular Expression is :
    /(^[A-Z]{5}[0-9]{4}[A-Z]{1}$)|(^[A-Z]{5}\s[0-9]{4}\s[A-Z]{1}$)|(^[A-Z]{5}-[0-9]{4}-[A-Z]{1}$)/

    and whereas my Aadhar Regular Expression is :
    /(^[0-9]{4}[0-9]{4}[0-9]{4}$)|(^[0-9]{4}\s[0-9]{4}\s[0-9]{4}$)|(^[0-9]{4}-[0-9]{4}-[0-9]{4}$)/

    Can these be suitably modified to help me achieve what I am looking for? Can someone kind here help me please?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @rohitatwp

    The regular expressions you enter through the fields’ settings are used to validate the fields’ values, not to format them.

    I’ll try to describe the process of using a regular expression to format a field.

    Assuming you have the fieldname1 field where you want to accept values with nine characters like:

    XXX-XXX-XXX

    The separator can be a hyphen or dot symbol or no separator at all.

    But no matter the format of the value entered by the user, you want to format it as XXX-XXX-XXX.

    The regular expression to validate the value entered by the user would be:

    /^[a-x0-9]{3}[\-\.]?[a-x0-9]{3}[\-\.]?[a-x0-9]{3}$/i

    But now, if you want to format the field’s value dynamically, you must insert an “HTML Content” field in the form with a piece of code similar to:

    
    <script>
    jQuery(document).on('change', '[id*="fieldname1_"]', function(){
        var v = new String(this.value);
        if(/^[a-x0-9]{3}[\-\.]?[a-x0-9]{3}[\-\.]?[a-x0-9]{3}$/i.test(v))
        {
            this.value = v.replace(/^([a-x0-9]{3})[\-\.]?([a-x0-9]{3})[\-\.]?([a-x0-9]{3})$/i, "$1-$2-$3");
        }
    });
    </script>
    

    If you need a custom coding service to implement your project, you can contact me through my private website: Click Here

    Best regards.

    Thread Starter Rohit Katre

    (@rohitatwp)

    Hello @codepeople

    I did follow your above Regular Expression. In fact, I customized it a bit as follows :
    /^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i
    thereby allowing a SPACE as well along with the HYPHEN or a DOT
    and all that works fine.

    However, the HTML Content field as suggested by you doesn’t seem to work. So I further tweaked it a bit to following :

    <script>
    jQuery(document).on('change', '[id*=&quot;fieldname6_&quot;]', function(){
    var v = new String(this.value);
    if(/^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i.test(v))
    {
    this.value = v.replace(/^[A-Z]{5}[\-]?[0-9]{4}[\-]?[A-Z]{1}$/i, &quot;$1-$2-$3&quot;);
    }
    });
    </script>

    since I wanted to dynamically capitalize the characters as well as add the HYPHEN alone. But that doesn’t seem to work. Earlier, I had forgotten to change the fieldname1 to my fieldname6 but even after doing that it did not work. I think the HTML Content field is the problem.

    Requesting you to kindly help please.

    Plugin Author codepeople

    (@codepeople)

    Hello @rohitatwp

    No, I’m sorry, you are not using the regular expression for replacement as you should.

    If you check my hypothetical example, the regular expression for replacement is a little different from the regular expression I used to validate the value. I’ve enclosed between parentheses the components to use with the special vars $1, $2, and $3

    Best regards.

    Thread Starter Rohit Katre

    (@rohitatwp)

    Sorry @codepeople

    I made a mistake in copying the code from the mail and therefore the double quotes (“) got replaced with (").

    I fixed that. And it has begun working but the problem is after replacement instead of giving the Capitalized and Hyphenated String it is dynamically returning “$1-$2-$3” for anything that I enter there.

    My HTML Content Code is :

    <script>
    jQuery(document).on('change', '[id*="fieldname6_"]', function(){
    var v = new String(this.value);
    if(/^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i.test(v))
    {
    this.value = v.replace(/^[a-xA-Z]{5}[\-\.\s]?[0-9]{4}[\-\.\s]?[a-xA-Z]{1}$/i, "$1-$2-$3");
    }
    });
    </script>

    What’s wrong now? Can you kindly help?

    Thanks

    Plugin Author codepeople

    (@codepeople)

    Hello @rohitatwp

    As I told you in the previous entry, please, look at the parentheses use in my code. If you don’t use parentheses, the replace method cannot identify the constants $1, $2, and $3 to use for replacement.

    This was my code:

    v.replace(/^([a-x0-9]{3})[\-\.]?([a-x0-9]{3})[\-\.]?([a-x0-9]{3})$/i, "$1-$2-$3");

    This question is not specific to our plugin. Please, I recommend you to read any of the regular expressing tutorials available for free on the Internet.

    Best regards.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Regular Expression – Indian PAN & Aadhar Card’ is closed to new replies.