views:

50

answers:

3

Hi friends,

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.

+2  A: 

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?");
    }
}
ChaosPandion
Hi Pandion, User can give anything but it should contain the word "india"
naveenkumar
Ok Chaos, thank you for your code and your time. Tomorrow i will come with some other questions. Bye.
naveenkumar
A: 

do you mean when the page loads to have the word 'India' in your textarea and it can be written over? or do you want it there permanent?

Drewdin
No no consider that textarea is getting address. At the time of submission i need to check whether the textarea is having the word "india" or not
naveenkumar
A: 
<textarea onchange='if(this.value.indexOf("india") == -1) this.value = "";'></textarea>
elektronikLexikon