[英]Restrict a input to only specific character and 6 digit of numbers
I want to restrict an input to only One Character (R) and 6 numbers only... Eg: R123896 我想将输入限制为仅一个字符(R)和6个数字...例如:R123896
If Input length increase 7 it will remove that digit and if First word is not R , it should replace that character to "R" 如果输入长度增加7,它将删除该数字,并且如果第一个单词不是R,则应将该字符替换为“ R”
I have written this script, but don't know how to move forward and shape it to what i want... 我已经写了这个脚本,但是不知道如何前进并将其塑造成我想要的...
$("#consultationident").keyup(function(key){
var txtVal = $(this).val();
if(isNumber(txtVal) && txtVal.length>6)
{
$(this).val(txtVal.substring(0,6) )
}
});
Please help! 请帮忙!
The use of a regular expression would be an idea... 使用正则表达式将是一个主意。
$("#consultationident").keyup(function(key){ // Uppercase the first character. var firstChar = $(this).val().substr(0,1).toUpperCase(); var rest = $(this).val().substr(1); $(this).val( firstChar + rest ); var txtVal = $(this).val(); var pattern = /^(R)(\\d{6})$/; // Check if the value entered fits the pattern. if(pattern.test(txtVal)){ console.log("Value ok"); }else{ console.log("Value wrong"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="consultationident">
here is the code for validating input using regex 这是使用正则表达式验证输入的代码
$("#consultationident").keyup(function(key){
var txtVal = $(this).val();
if(txtVal.length>6)
{
str = $(this).val().substring(0,7);
pattern = /^[a-zA-Z]{1}(\d{6})$/
if(pattern.test(str)) {
str = str.replace(/^[a-zA-Z]/,"R") // Replace any character with "R"
console.log(str);
}
else {
console.log("invalid input");
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.