Rabhat
Rabhat

Reputation: 1

asp.net text box text format validation using JavaScript

I have one requirement,user should enter text in asp.net text box only 'X-XXXX' format.user can enter text or numbers but should be this format. Can any one suggest JavaScript validation function

Upvotes: 0

Views: 266

Answers (1)

thalisk
thalisk

Reputation: 7743

Assuming you have this HTML:

 <input id="foo"><span id='validationMessage'></span>

This javascript does the job:

var input = document.getElementById('foo');
var spanMessage = document.getElementById('validationMessage');
input.addEventListener('blur', function () {
    if (!input.value.match(/[a-z0-9]-[a-z0-9]{4}/i)) {
        if (spanMessage.firstChild) {                                                                                                                                                                                         
            spanMessage.removeChild(spanMessage.firstChild);
        }
        spanMessage.appendChild( document.createTextNode("Invalid input; must be X-XXXX"));
    } else {
        spanMessage.removeChild(spanMessage.firstChild);
    }
}, false);

Upvotes: 2

Related Questions