简体   繁体   English

JavaScript表单验证-需要特定字段输入,但大小写不同

[英]JavaScript Form Validation - specific field entry needed but in upper/lower case

I have a simple form with a single field which essentially works like a password, only letting a user to the next page when they click submit, if the correct 'referral code' is entered in the field. 我有一个具有单个字段的简单表单,该字段本质上类似于密码,如果在该字段中输入了正确的“引荐代码”,则仅允许用户单击提交时进入下一页。

I need the my JS validation code to allow the word 'garlic' be entered in upper or lower case or a mixture of both to pass the validation. 我需要我的JS验证代码,以允许以大写或小写形式输入“大蒜”一词,或将两者混合使用以通过验证。 Only 'garlic' can be used as the referral word though. 但是,只能将“大蒜”用作推荐词。 The below code is what I have currently but it only specifically validates 'garlic' in all lower-case. 下面的代码是我目前使用的代码,但仅专门验证所有小写字母中的“大蒜”。

Any assistance in being able to validate one word in upper and lower-case and both together would be much appreciated. 能够对一个单词以大写和小写以及两者同时进行的任何帮助将不胜感激。

function ValidateContactForm() {
    var codeentry = document.form1.code;
    if (codeentry.value == "") {
        window.alert("Our apologies, the code does not match our records; please call us");
        codeentry.focus();
        return false;
    } else if (codeentry.value !== "garlic") {
        alert("Our apologies, the code does not match our records; please call us.");
        codeentry.focus();
        return false;
    }
    return true;
}

将两者都设为大写(或小写),然后进行比较。

codeentry.value.toUpperCase() !== "GARLIC"

使用以下条件

else if (codeentry.value.toLowerCase() !== "garlic")

Note the toLowerCase() after the value! 注意值后面的toLowerCase()!

function ValidateContactForm()
 {
var codeentry = document.form1.code;

if (codeentry.value == "")
{
    window.alert("Our apologies, the code does not match our records; please call us");
    codeentry.focus();
    return false;

}

else if (codeentry.value.toLowerCase() !== "garlic" ) {

    alert("Our apologies, the code does not match our records; please call us.");
    codeentry.focus();
    return false;

}


 return true;
}

Try something like this 试试这个

else if (codeentry.value.toUpperCase() !== "garlic".toUpperCase() ) {

alert("Our apologies, the code does not match our records; please call us.");
codeentry.focus();
return false;
}

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

相关问题 UPPER或小写的JavaScript / HTML? - JavaScript/HTML in UPPER or lower case? javascript:验证大写和小写 - javascript : validate upper case and lower case 如何为JavaScript中的第一个字母的大写字母和其余的小写字母写正则表达式 - how to write regular expression for 1st letter upper case and remaining are lower case for validation in javascript (Javascript)搜索特定项目时如何忽略在搜索栏中注册的小写/大写 - (Javascript) How do you ignore lower/upper case being registered in search bars when searching for a specific item 在内部类中使用Javascript将方法转换为小写和大写 - Convert to lower case and upper case , inside class with method using Javascript 使用php验证大写和小写形式的现有数据 - validation for existing data in both upper and lower case in using php 小写的纯Javascript拆分字符串UPPER - Plain Javascript Split String UPPER from lower case 将 javascript 中的所有其他字母转换为大小写 - Converting every other letter to upper and lower case in javascript 如何忽略数组检查javascript中的大写/小写 - how to ignore upper/lower case in array check javascript Javascript 识别句子,无论句子中的任何位置大小写 - Javascript recognize sentence regardless of upper and lower case anywhere in the sentence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM