[英]Using JQuery, how do I check that a string is a letter character followed by eight digits?
I'm a beginner writing JQuery to add to a customization form website. 我是初学者,正在编写JQuery以添加到自定义表单网站。 Most of the options are drag and drop, but I have to write custom jquery in some cases.
大多数选项都是拖放操作,但是在某些情况下,我必须编写自定义jquery。
For this, I've been able to figure out to validate a nine-character string so that an error message is presented if the string is NOT 9 characters long, and if it starts with anything other than "B", "E", or "N." 为此,我已经能够验证一个九个字符的字符串,以便如果该字符串的长度不为9个字符,并且以“ B”,“ E”以外的任何其他字符开头,则会出现错误消息,或“ N”。
However, it also needs to check and make sure that all other characters after the first is a digit. 但是,它还需要检查并确保第一个字符之后的所有其他字符都是数字。 For instance, an acceptable user input would be e00012345.
例如,可接受的用户输入为e00012345。
What is the simplest way to do this? 最简单的方法是什么?
// this validation will check to make sure that an accepted value is entered into a field. // currently, the validation is not perfect. Ideally, the value would start with a specific character (n, b or e) and 8 digits. Right now it just must start with n, b or e and be 9 characters long. $(function() { // for the Missouri Business Number -- on blur, if the value is 9 characters long and starts with b, e, or n (uppoer or lower case), then the input is valid. Otherwise, error messages appear. $("input#id_wrT4duNEOW").blur(function() { if ( (($("#id_wrT4duNEOW").val().startsWith("b")) && ($("#id_wrT4duNEOW").val().length == 9)) || (($("#id_wrT4duNEOW").val().startsWith("e")) && ($("#id_wrT4duNEOW").val().length == 9)) || (($("#id_wrT4duNEOW").val().startsWith("n")) && ($("#id_wrT4duNEOW").val().length == 9)) || (($("#id_wrT4duNEOW").val().startsWith("B")) && ($("#id_wrT4duNEOW").val().length == 9)) || (($("#id_wrT4duNEOW").val().startsWith("E")) && ($("#id_wrT4duNEOW").val().length == 9)) || (($("#id_wrT4duNEOW").val().startsWith("N")) && ($("#id_wrT4duNEOW").val().length == 9)) ) { // good things happen } else { // error message } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
EDIT 编辑
Okay, I tried adding in the regex line, but I'm not getting the result. 好的,我尝试添加正则表达式行,但是没有得到结果。 What am I missing?
我想念什么?
$(function() {
$("input#id_wrT4duNEOW").blur(function() {
const regex = /^[bBeEnN]{1}[0-9]{8}$/
var mobiz = $("#id_wrT4duNEOW").val();
if (console.log(regex.test(mobiz)))
{
// good things happen
}
else {
// error message
}
});
});
Regex to the rescue. 正则表达式可以解救。 It's pretty straightforward to do using a regex and its associated
.test
method. 使用正则表达式及其关联的
.test
方法非常简单。 The following regex ensures the string starts with one of the characters b
, e
, or n
(not case sensitive), followed by exactly 8 digits: 以下正则表达式可确保字符串以字符
b
, e
或n
(不区分大小写)之一开头,然后紧跟8位数字:
test1 = "B12345678"; test2 = "N123456789"; test3 = "x12345678"; const regex = /^[bBeEnN]{1}[0-9]{8}$/ console.log(regex.test(test1)) console.log(regex.test(test2)) console.log(regex.test(test3))
So, for your snippet, you could adapt it like this: 因此,对于您的摘要,您可以像这样修改它:
$(function() { $("input#id_wrT4duNEOW").blur(function() { var val = $("#id_wrT4duNEOW").val(); if (/^[ben]{1}\\d{8}$/i.test(val)) { // good things happen } else { // error message } }); });
//1) must be 9 characters //2) first character must be B, E, or N //3) 8 characters past the first must be digits var pattern = /^(B|E|N)\\d{8}$/ console.log(pattern.test("weee")); console.log(pattern.test("B12345678")); console.log(pattern.test("A12345678")); console.log(pattern.test("B123456789"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.