数字和字符组合的Javascript验证 [英] Javascript validation for number and character combination

查看:45
本文介绍了数字和字符组合的Javascript验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个组合DE55680501010022061234

在这个组合中,前两个字母是DE,后跟20个数字,总共22个长度。如何使用JavaScript检查这种格式组合。

I have this combination " DE55680501010022061234 "
In this combination, first two letter is " DE " and followed by 20 numbers, and total of 22 length. How to check this format combination using JavaScript.

推荐答案

您可能会尝试做您需要的事情,并分享您尝试过的问题代码。话虽如此,您可以使用以下内容:



您可以从以下代码中选择正则表达式方法或非正则表达式方法。



It is expected that you would attempt to do what you need and share the code you have tried with the question. Having said that, here is something you can make use of:

You can choose from either regex method or non-regex method from following code.

function Validate() {
           var inputString = 'DE55680501010022061234';
           // using regex
           var regEx = /DE\d{20}


/;
if (regEx.test(inputString)== false ){
alert (' 不对!'
}

// 没有正则表达式
如果( inputString.length!= 22 ){
alert(' 不对!
}

if (inputString.charCodeAt( 0 )!= 68 || inputString.charCodeAt( 1 ) != 69 ){
// not not D或E
alert(' 不对!'
}

for var i = 2 ; i < 22 ; i ++){
if (!(inputString.charCodeAt(i)> 47 && inputString.charCodeAt(i)< 58 )){
alert(' 不对!'
}
}
}
/; if (regEx.test(inputString) == false) { alert('Not right!') } // without regex if (inputString.length != 22) { alert('Not right!') } if (inputString.charCodeAt(0) != 68 || inputString.charCodeAt(1) != 69) { // not D or E alert('Not right!') } for (var i = 2; i < 22; i++) { if (!(inputString.charCodeAt(i) > 47 && inputString.charCodeAt(i) < 58)) { alert('Not right!') } } }


尝试使用以下代码:

Try with below code:
<script>
function myFunction() {
    var str = "DE55680501010022061234";
    var pattern = /^DE[0-9]{20}


这篇关于数字和字符组合的Javascript验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆