1

We are working on a form validation code snippet where jQuery masking plugin has been used to enforce validation on user input.

Masking Pattern used with zip code field is 99999?-9999 where everything is optional after "?".

Problem occurs when user fill up the form using autofill feature in chrome[i.e. double click on input field and select value] and submit the form. On form submission, zip code value is coming as 23455-____ which includes masking pattern characters as well i.e. hypen & underscore.

Refer attached screenshot after using autofill feature. http://inft.ly/3mmtNdA

If optional characters contains Hypen (-) and underscore(_) then those needs to be removed before submitting it to server. I am trying to use regex but didn't find anything which checks for specific characters after 5th index item and then remove those.

Any help would be really helpful.

9
  • what do you have so far? Can you give a few examples for a valid and unvalid zip codes (with special & unusual cases) Commented Jun 16, 2015 at 10:45
  • use on input function and then take the string into a javascript variable and then use str_replace for removing hyphen and underscore Commented Jun 16, 2015 at 10:47
  • as masking pattern written, first zip code must contain atleast 5 digits and after that everything is optional. Masking plugin prevent user to input anything less then 5 digits. Commented Jun 16, 2015 at 10:47
  • @sonamgupta you are right but i am struggling to find a regex which removes specific characters after 5th index item Commented Jun 16, 2015 at 10:49
  • str = str.replace(/_|-/g, ""); use this here str is your string Commented Jun 16, 2015 at 10:50

2 Answers 2

2

As you first five characters are numbers you can catch those in a capture group. Check if those are followed by (-) or (_) and replace those.

You would do this with the following regex:

str.replace(/(\d{5})[\-_](.*)/, '$1$2');

First you create a capture group to save the first five digits (\d{5}). Then you look for the characters you want to delete [\-_]. After that you capture the rest of your string (.*).

Now you can replace your string with the to capture groups '$1$2' and you are done.

Sign up to request clarification or add additional context in comments.

1 Comment

it is not always first five characters are digits. It depends on the masking pattern applied on zip code. For ex. canadian zip code is alphanumeric. So, i need to read masking pattern first to get the index of "?" then pass this index to regular expression to remove special characters after index value. To overcome this combinations, i was searching for a regex pattern which returns string after 5th index value. For e.g. if string is "23345-____" then that regex should search for "-" & "" after 5th digit.
1

You could simply strip the trailing underscores or dashes from the string like so:

var str = '12345-_____';
str.replace(/[-_]+$/, ''); // "12345"

var str = '12345-123__';
str.replace(/[-_]+$/, ''); // "12345-123"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.