How do i ensure that my textarea value should开发者_开发知识库 always contain the word "india" in javascript?
Actually i want to validate the textarea so that the user should always the word "india" in that.
Please help me. Thanks in advance.
You can use the blur
event which is invoked when the user removes focus from the textarea
.
<textarea onblur="return validate(this);"></textarea>
var reg = /\bindia\b/im;
function validate(textArea) {
// case-insensitive search for "india"
if (!reg.test(textArea.value)) {
alert("Where's India?");
}
}
<textarea onchange='if(this.value.indexOf("india") == -1) this.value = "";'></textarea>
精彩评论