使用JavaScript的asp.net文本框文本格式验证

时间:2022-08-28 22:28:38

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

我有一个要求,用户应该在asp.net文本框中输入文字'X-XXXX'format .user可以输入文字或数字,但应该是这种格式。任何人都可以建议JavaScript验证功能

1 个解决方案

#1


2  

Assuming you have this HTML:

假设你有这个HTML:

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

This javascript does the job:

这个javascript做的工作:

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);

#1


2  

Assuming you have this HTML:

假设你有这个HTML:

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

This javascript does the job:

这个javascript做的工作:

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);