- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的护照代码有一个文本框
。
如果用户输入的代码与正则表达式不同,我该如何定义以防止继续在文本框上书写?
并保留错误直到用户输入正确的错误?这是我的片段:
$(".passno").each(function(index, element) {
$(this).bind('keyup', function(event) {
var regex = new RegExp("^[A-Z][0-9]{8}$");
var value = event.currentTarget.value;
if (!regex.test(value)) {
$(this).next(".box-infoes").find(".error").css("display", "block")
} else {
$(this).next(".box-infoes").find(".error").css("display", "none")
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="text" class="passno" placeholder="e.g.=J12345678" />
<div class="box-infoes">
<div style="color:red; display:none;" class="error"> This Is a Wrong passportcode !</div>
</div>
最佳答案
你的正则表达式应该像 RegExp("[A-Z]{1}[0-9]{8}")
但你的代码需要更多修改,这样可以防止一些按钮像 SHIFT CTRLAS
字符或如果它为空则删除错误等。
现在正则表达式说:[A-Z]{1}
一个字 + [0-9]{8}
八个数字。
示例类型 J12345678
$(".passno").each(function(index, element) {
$(this).bind('keyup', function(event) {
var regex = new RegExp("[A-Z]{1}[0-9]{8}");
var value = event.currentTarget.value;
$(this).val($(this).val().toUpperCase());
if (!regex.test(value) || value.length > 9) {
$(this).next(".box-infoes").find(".error").css("display", "block")
} else {
$(this).next(".box-infoes").find(".error").css("display", "none")
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="text" class="passno" placeholder="e.g.=J12345678" maxlength="9"/>
<div class="box-infoes">
<div style="color:red; display:none;" class="error"> This Is a Wrong passportcode !</div>
</div>
好吧,为了限制字符长度,您可以在 input
上使用 maxlength="9"
或使用 jquery
value 控制它.length > 9
关于javascript - 如果模式针对正则表达式,则防止继续在文本框上书写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605117/
我正在开发 GPA 计算器,而且我是 C 语言新手。我在 Ubuntu 12 中使用 gvim 编写此代码,并在终端中使用 gcc 进行编译。 这是我到目前为止的代码。我还想包含一种检查方法,以确保用
我正在使用tinymce 编辑网络应用程序中的某些字段。我需要一个带有一些规范的 html 结果(编辑后)。 例如:当我按 Entertinymce 时,创建一个新段落(没关系,我知道可以更改此行为,
我正在尝试在EmguCV(OpenCV C#包装器)图像上绘制多于一行的字符串。但它表明EmguCV无法识别新行“\ r \ n”字符。 如何实现呢?另外,如何获取文本高度,以便可以手动设置下一个字符
我有一个 richTextBox,一个人将在其中阅读患者的医疗记录。 我添加了一个按钮,用于将今天的日期(以 DD/MM/YYYY 格式)添加到 richTextBox 中。当有人需要更新病人的记录时
我是一名优秀的程序员,十分优秀!