Design a site like this with
Get started

Function to validate CNIC using Regex


In JQuery

    var checkCNIC = function (textBox) {


                 var regexp = new RegExp('^[0-9+]{5}-[0-9+]{7}-[0-9]{1}$');
                 var check = textBox.value;
                if (!regexp.test(check)) {

                    alert('Invalid CNIC');
                    $(textBox).css('border-color', 'red');
                    return false;

                else {
                    $(textBox).css('border-color', 'green');
                    $(textBox).value = check;
                    return true;


    @Html.TextBox("AddCNIC", null, new { @class = "form-control text-box", @placeholder = "11111-1111111-1", @onchange = "checkCNIC(this)" })


This regex will allow you to enter the CNIC in this format 12345-1234567-1

i.e. 5 numbers then a dash followed by 7 numbers and a dash and at last a number


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at

Up ↑

%d bloggers like this: