Zip code Validation Format

Hello Everyone, PLEASE HELP!

I have project creating an Online Form, however, ran to a problem. I want to add the charector “-” in the middle of whatever they enter. it must be 9 digits. This field should allow a special character to be entered. Basically, the user can enter any special character into this field and it will append the “-”. For example, user can enter “12-3456789” and it will automatically display as “12345-6789”.

Java Script:

applicantAddressZipCode.subscribe(function (newValue) {
        if (newValue != undefined && newValue != '') {

            applicantAddressZipCode(formatZipCode(newValue));
            var dataValue = String(applicantAddressZipCode().replace(/(^\d{5}$)|(^\d{5}-\d{4}$)/, ''));
            if (dataValue > 9999999999999.99 || dataValue < 0) {
                applicantAddressZipCode('');
            }
            if (loading == false) {
                sendCommand('SAVE');
            }
        }
    });

    function formatZipCode(value) {
        var Z = /(\d{5})(\d+)/;
        if (value != undefined && value != '') {
            return value = value.replace(Z, '$1' + '-' + '$2');;
        }
        else {
            return value;
        }
    };

HTML:

<input type="text"   maxlength="9" id="applicantAddressZipCode"  class="form-control text-font-md" data-bind="css: applicantAddressZipCodeCSS, attr: { title: applicantAddressZipCodeToolTip }, event: {focusout: checkLostFocus.bind($root, $data, 'applicantAddressZipCode')}, value: applicantAddressZipCode">

it allows one charector, but the charector has been counted as a digit which it shouldn’t be. I want to put that charector in the proper place in case has been entered. PLEASE HELP!

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Why don’t you just

  1. Grab their data input
  2. Strip it of all non-numeric characters (leaving you only with digits 0–9)
  3. Get the first 5 characters, append your dash “-”, and append the rest of the input string

Hi,
First, thank you for your response!

I can’t see your code?

This is what I’m trying to do, but I don’t seem to be able to do that. Could you please modify my code so I can see?

Thanks a lot in advance !

@Anmar0293 - He is trying to give you a hint so you can figure it out yourself. Give it a try and repost what you have if you still can not figure it out.

So I came up with this. it works fine, but if the user enters a “-” it would be counted as a digit. Since I’m maximized with 9 digits, with the “-” user enters it would be 8.

function formatZipCode(value) {
value = value.replace(/[^/\d]/g,’’);
var Z = /(\d{5})(\d)/;
if (value != undefined && value != ‘’) {
return value = value.replace(Z, ‘$1’ + ‘-’ + ‘$2’);;
}
else {
return value;
}
};

[So I came up with this. it works fine, but if the user enters a “-” it would be counted as a digit. Since I’m maximized with 9 digits, with the “-” user enters it would be 8.

function formatZipCode(value) {
value = value.replace(/[^/\d]/g,’’);
var Z = /(\d{5})(\d)/;
if (value != undefined && value != ‘’) {
return value = value.replace(Z, ‘$1’ + ‘-’ + ‘$2’);;
}
else {
return value;
}
};

Please read PortableStick’s post about formatting code and go to that link. He spends a lot of time on a one man crusade to get everyone to make their code look better in the forum.

It may seem trivial, but some of us are so sick of looking at poorly formatted code (makes it sooooo much harder to understand) that we sometimes spend less time helping or sometimes skip entirely things that are too difficult to understand. A few extra keystrokes on your end will make in monumentally easier for people to help you.

it would be counted as a digit. Since I’m maximized with 9 digits, with the “-” user enters it would be 8.

Shouldnt be your input form field maxlength be 10?
12345-1234