简体   繁体   English

使用JQuery,如何检查字符串是字母字符,然后是八位数字?

[英]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: 以下正则表达式可确保字符串以字符ben (不区分大小写)之一开头,然后紧跟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.

相关问题 如何检查字符串是否包含至少一个数字,字母和既不是数字也不是字母的字符? - How do I check if a string contains at least one number, letter, and character that is neither a number or letter? 仅当字符串后跟大写字母时,如何替换字符串中的字符? - How can I replace a character in a string only if it's followed by a uppercase letter? 如何检查字符串是否包含某个字母,后跟除自身或元音以外的任何字母? - How can I check if string contains a certain letter followed by anything other than itself or vowels? 如何检查我的字符串是否以 WA 后跟 9 位数字结尾 - How do I check if my string ends with WA followed by 9 digit 如何在正则表达式中将单个S或E字符串中的字符串拆分后跟JavaScript中的数字? - How do I split a string in regex by a single S or E char followed by digits in JavaScript? 如何检查字符串中是否包含 ASCII 字母? - How do I check if a string has an ASCII letter in it? 如何检查字符串中的字符是否是 JavaScript 中的特定字符? - How do I check whether a character in a string is a specific character in JavaScript? 如何为由变量名称后跟Javascript中的任何字符组成的字符串创建正则表达式? - How do I create a regex for a string made up of a variable name followed by any character in Javascript? 查找所有以特定字符开头的单词,然后是字符串中的数字 - find all word start with specific character followed by digits in string 如何使用正则表达式在字符串的末尾获取数字,后跟该字符`:` - How to get a number at the end of a string followed by this character `:` using regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM