简体   繁体   English

如何使用JavaScript验证输入?

[英]How to validate input using JavaScript?

I am to check the number of occurrences of a numeric value in a string ie in that string only 2 numeric characters are allowed. 我要检查一个字符串中数字值出现的次数,即在该字符串中仅允许使用2个数字字符。 Can anyone help me to solve this? 谁能帮我解决这个问题?

Thanks in advance 提前致谢

Try this code 试试这个代码

    var s ="1asds3asa"; // string to check
    var t = s.replace(/[^\d]/g,""); // a string with everything but the digits removed
    if(t.length>2)
    {
       alert("Only 2 numbers allowed");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM