How to show incorrect/correct inputfield by backgroundcolour from jQuery to HTML

50 Views Asked by At

I have to make a patientsform (made in HTML) and ask a few personal questions. For example: a patient number. I have used an input field where someone can type their number. The number has to be exactly 8 numbers. If this is true, the input field has to turn green, if not, it needs to give an alert and turn the input field red. This is the code I have so far:

function checkpnummer() {

 if(!$('#patientnummer').val().match(/^\d{8}$/)) {
  $('#patientnummer').css({"background-color": "#CD5C5C"});
  alert("Vul een patientnummer in dat bestaat uit 8 cijfers.");
 }
 else {
  $("#patientnummer").css("background-color", "#00FA9A");
  $("#patientnummer").focusout();
 }
}
}

However, If I try to fill in a number in my inputfield (in HTML) it does not make a difference between a correct or an incorrect input. Could someone help me solve my problem?

1

There are 1 best solutions below

1
On

Your first css call doesn't need at all to be an object. Make it just like your second one ($(selector).css("background-color",value)).