简体   繁体   English

有没有办法使Regex函数检查名字和姓氏?

[英]Is there a way to make a Regex function to check a first and last name?

I'm currently doing an assignment for my coding class (I'm a beginner). 我目前正在为我的编码课做作业(我是初学者)。 The assignment is to create an HTML page with 2 inputs, one for a First Name, one for a Last Name, and to make a submit button that, when clicked, checks to see if the First and Last Names both start with an uppercase letter and contain at least 1 character. 任务是创建一个具有2个输入的HTML页面,一个用于名字,一个用于姓氏,并创建一个提交按钮,单击该按钮可检查名字和姓氏是否都以大写字母开头且至少包含1个字符。 When they both match the regex, an alert is used and the console logs. 当它们都与正则表达式匹配时,将使用警报,并且控制台将记录日志。 When they don't, a different alert is used, and the console doesn't log. 如果不这样做,则会使用其他警报,并且控制台不会记录日志。 When I finished making the html and the script, I typed in a name in both inputs that should match the regex, but it only issues an alert for an incorrect input. 当我完成html和脚本的编写后,我在两个输入中都键入了一个名称,该名称应与正则表达式匹配,但是它只会针对不正确的输入发出警报。 There is something wrong, but I can't find it. 有问题,但我找不到。


function regexChecker(firstName, lastName) {
    firstName = document.getElementById("firstName").innerHTML;
    lastName = document.getElementById("lastName").innerHTML;
    let firstNameRegex = /^[A-Z][a-z]*$/;
    let lastNameRegex = /^[A-Z][a-z]*$/;
    if (firstName.match(firstNameRegex) && lastName.match(lastNameRegex)) {
        alert('Yay! Your inputs were all correct!' );
        console.log(true);
    }
    else {
        alert('Oh no! Thats an invalid format!' );
        console.log(false);
    }
}

In the linked html that I didn't provide, there are two inputs for each name, as well as a button. 在我未提供的链接html中,每个名称都有两个输入以及一个按钮。 I inputted the name Isaac in the first one and Daniels in the second. 我在第一个输入了以撒的名字,在第二个输入了丹尼尔斯的名字。 I expected the alert to be "Yay! Your inputs were all correct!", but I instead got "Oh no! Thats an invalid format!" 我希望警报为“是!您的输入都正确!”,但我却收到了“哦,不!那是无效的格式!”

You are making two mistakes: 您犯了两个错误:

  • Use value instead of innerHTML . 使用value代替innerHTML <input> doesnot have innerHTML <input>没有innerHTML
  • match returns an array which is always truthy. match返回一个始终为真的数组。 Even Boolean([]) => true . 甚至Boolean([]) => true You should use test() instead of match . 您应该使用test()而不是match
  • Your RegExp will not pass a string containing capital letter after first. 您的RegExp不会传递包含大写字母的字符串。 You should use /^[AZ][a-zA-Z]*$/ 您应该使用/^[AZ][a-zA-Z]*$/

Here is the code. 这是代码。

function regexChecker(firstName, lastName) {
    firstName = document.getElementById("firstName").value;
    lastName = document.getElementById("lastName").value;
    let firstNameRegex = /^[A-Z][a-zA-Z]*$/;
    let lastNameRegex = /^[A-Z][a-zA-Z]*$/;
    if (firstNameRegex.test(firstName) && lastNameRegex.test(lastName)) {
        alert('Yay! Your inputs were all correct!' );
        console.log(true);
    }
    else {
        alert('Oh no! Thats an invalid format!' );
        console.log(false);
    }
}

Note :Both the strings have same regex /^[AZ][az]*$/ so you could use every() to check on both. 注意 :两个字符串都具有相同的正则表达式/^[AZ][az]*$/因此您可以使用every()来检查两者。

function regexChecker(firstName, lastName) {
    firstName = document.getElementById("firstName").value;
    lastName = document.getElementById("lastName").value;
    let regex = /^[A-Z][a-zA-Z]*$/;
    if ([firstName,lastName].every(x => regex.test(x))) {
        alert('Yay! Your inputs were all correct!' );
        console.log(true);
    }
    else {
        alert('Oh no! Thats an invalid format!' );
        console.log(false);
    }
}

You can use this regexp: 您可以使用此正则表达式:

/^([A-Z][a-z]*)+$/

It will test that it starts with an uppercase letter, and is followed by lower case letters, and is at least one letter long. 它将测试它以大写字母开头,然后是小写字母,并且至少一个字母长。

You can use the regexp with .test() to get a true/false value. 您可以将regexp与.test()以获取true / false值。

 let regexp = /^([AZ][az]*)+$/ document.names.addEventListener('submit', e => { e.preventDefault() let firstValid = regexp.test(document.names.first.value) let lastValid = regexp.test(document.names.last.value) // Sanity check console.log(firstValid, lastValid) if (!firstValid) alert('First name is invalid!') else if (!lastValid) alert('Last name is invalid!') else alert('Both are valid!') }) 
 <form name="names"> <input type="text" name="first" placeholder="First Name"> <input type="text" name="last" placeholder="Last Name"> <p> <input type="submit" value="Check Names"> </p> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM