简体   繁体   English

检查文本字段值是否匹配

[英]Checking textfield values if they are matching

Im making a little form where you can pick your value. 我正在制作一个小表格,您可以在其中选择自己的价值。

If the value is not filled in correctly(I have given some example answers) I want an alertbox to pop-up which indicates the incorrectly filled in textfields. 如果未正确填写值(我给出了一些示例答案),我希望弹出一个警告框,指示未正确填写文本字段。

Cant seem to make it work... This is what I tried 似乎无法使其工作...这是我尝试的

if(document.getElementById("BS").value.match ("thin","thick","medium")
{
    alert("Fill in the correct values");
}

Here is the fiddle. 这是小提琴。 http://jsfiddle.net/vuc8Z/ http://jsfiddle.net/vuc8Z/

Here is my suggestion: 这是我的建议:

var val = document.getElementById("BD").value;
 if (!(val == "thin" || val =="thick" || val =="medium")) {
    alert("Vul een geldige waarde in bij de aangegeven velden");
}

Demo here 在这里演示

Notice that you were using getElementById("BS") , but you wanted BD i think. 请注意,您使用的是getElementById("BS") ,但我想使用BD I changed your match function to a triple || 我将匹配功能更改为三重|| (or) if statement. (或)if语句。

I moved your if statement to inside the "go" button click handler so it is fired when the button is clicked. 我将您的if语句移到了“ go”按钮单击处理程序的内部,因此在单击按钮时将其触发。

The match() function expects a regex, you can read more here ( MDN: .match() ). match()函数需要一个正则表达式,您可以在此处阅读更多信息( MDN: .match() )。

EDIT: 编辑:

To check if the color text input is in the right format you can use this: 要检查彩色文本输入的格式是否正确,可以使用以下命令:

var filter = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
if (!color.match(filter)) {
    alert('Color code Error!');
}

Updated demo here 在这里更新了演示

If you want to use match method then you can use like : 如果要使用match方法,则可以使用like:

   var str = document.getElementById("BD").value;
   if(str.match('thin') || str.match('thick') || str.match('medium')) 
   {
     alert("Fill in the correct values");
   }

As per my knowledge match takes regular expression as parameter and don't take multiple values. 据我所知,match使用正则表达式作为参数,而不使用多个值。

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

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