问题
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