简体   繁体   中英

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. 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");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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