I am trying to create some simple validation for input. If the input has a value of "abc" I want to add a class red to the input and change text color to red. And somehow I want to stop submit the form.
Here is my code so far:
$(document).ready(function () {
$('#inputname').click(function () {
if ($("#inputname").val('abc') {
$("#inputname").addClass(red);
} else {
$("#inputname").removeClass(red);
}
});
});
HTML:
<input type="text" value="abc" name="inputname" id="inputname" class="inputname"
onblur="if (this.value == ``) {this.value = `abc`;}"
onfocus="if (this.value == `abc`) {this.value = ``;}" />
CSS:
.red { color: #FF0000;}
JSFiddle: https://jsfiddle.net/n5x4q0bp/