views:

36

answers:

5

how do I check if a field has a string value tht contains letters 'abc' containing in it using Jquery ??

I need to check a field that contains a string value, if the field contains the characters 'abc' I need to disable few fields. Please let me know how I can validate in jquery??

A: 
if($("#myInput").val() == "abc") {
   $("#myOtherInput").attr("disabled","disabled");
}
infinity
A: 

One way to go is like:

$('#text_id').keyup(function(){
   if (this.value === 'abc'){
       // disable other fields
   }
});

Or with vanilla javascript:

var el = document.getElementById('text_id');
el.onkeyup = function(){
   if (this.value === 'abc'){
       // disable other fields
   }
};

Where text_id is supposed to be the id of the element you want to check for.

Sarfraz
A: 

YOu dont need JQuery. do something like this

var s = "foo";
alert(s.indexOf("oo") != -1);

it'll tell you if the variable s contains the text "oo" or not.

TAkinremi
A: 
if($(":input[value*='abc']").length > 0) {
    // Disable fields
}

This will find you any input field that contains (written somewhere in the field value) abc, though obviously replace ":input" with whatever applies in your case.

Neil
instead of :input, should I be replcing it with #FieldName or :FieldName?
srao
#FieldName means you have an input with id="FieldName". :input is a special selector in jquery which selects all inputs and some non-inputs like textarea. So :FieldName wouldn't mean anything.
Neil
+1  A: 
var inputString = $('#inputfield').val(); //get value from field
if ((inputString.indexOf("abc") != -1) == true) {
   //disable fields here...
}

^_^

Neurofluxation