How could I request a certain number (digit) with a certain times to be inserted in an input element?

我是研究僧i 提交于 2020-01-25 07:26:38

问题


How could I request a certain digit with a certain times to be inserted in an input element?

I need to request from The users to input three times a certain digit, let we say number 9, in the text or password field, in any format..

How could I do that using HTML5 or any other way?

If I had this code:

<td><input type="password" name="password" maxlength="9" pattern="\d+" title="9 numbers only" placeholder="9 numbers only"  value="<?php echo htmlentities($password); ?>" /></td>

Then, my required pattern to be as example: ("x" is any digit)

xxx999xxx

Or: x9x9x9xxx

Or: 9xxx9xxx9

Or: x9xxxx99x

It doesn't matter the format, the matter is 3 times of number 9 to be inserted every time.


回答1:


You can use this tool for Regular Expressions: http://gskinner.com/RegExr/.

This pattern should work: [^9.]*9[^9.]*9[^9.]*9[^9.]*




回答2:


You can get the frequecy of the character in javascript like

    <script language="javascript" type="text/javascript">

        alert ('hi');
        var arr="sandeeep".split('');
        var uniqs = {};

        for(var i = 0; i < arr.length; i++) {
            uniqs[arr[i]] = (uniqs[arr[i]] || 0) + 1;
        }
         var countarr = { val: arr[0], count: 1 };
        for(var u in uniqs) {
            if(countarr.count < uniqs[u]) { countarr= { val: u, count: uniqs[u] }; }
        }
        alert( countarr.count);
    </script> 

from the above code

      alert( countarr.count);

you can check max count from countarr.count and validate it



来源:https://stackoverflow.com/questions/16828937/how-could-i-request-a-certain-number-digit-with-a-certain-times-to-be-inserted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!