简体   繁体   中英

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/

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. I changed your match function to a triple || (or) if statement.

I moved your if statement to inside the "go" button click handler so it is fired when the button is clicked.

The match() function expects a regex, you can read more here ( 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 :

   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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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