Reputation: 1505
I want to check the value of a textbox that whether it contains a particular set of strings or not. How do I do this? (preferably using jquery)
I have a textbox where a user enters something. I want to check whether the string he enters contains BOTH "@" and "port". Please help..
I tried using contains but it is giving false positives.
Here is the code I used.
if($("#id:contains('@'):contains('port')")) {
$("#1st").hide();
$("#2nd").show();
} else {
alert("Wrong value entered.");
}
Upvotes: 1
Views: 8189
Reputation: 67
function check()
{
var val = frm1.uname.value;
//alert(val);
if (val.indexOf("@") > 0)
{
alert ("email");
document.getElementById('isEmail1').value = true;
}else {
alert("usernam");
document.getElementById('isEmail1').value = false;
}
}
Upvotes: 1
Reputation: 2335
Try a combination of javascript and jQuery
var txtVal = $("#id").attr('value');
if(txtVal.indexOf("@")!= -1 && txtVal.indexOf("port")!= -1)
alert('yes');
else
alert('no');
Upvotes: -1
Reputation: 318708
Use $('#id').val()
to get the value. Then you can use regular JavaScript functions to check if it contains certain values.
var value = $('#id').val();
if(~value.indexOf('@') && ~value.indexOf('port')) {
$("#1st").hide();
$("#2nd").show();
} else {
alert("Wrong value entered.");
}
In case you wonder what the ~
does: it's a smart way to check for != -1
as ~-1 == 0
.
Upvotes: 2