Forum Moderators: open

Message Too Old, No Replies

jquery regex conditional syntax

         

neophyte

4:36 pm on Apr 7, 2011 (gmt 0)

10+ Year Member



Hello All -

I've been doing a lot of php but - due to a client request - am now thrown in to the deep end regarding some jquery functionality.

I've partially got this little script working, but am stuck on the conditional syntax involved in checking a form field's content against a regular expression.

I've put ***what I think*** is the problem area in bold.

Be aware that I know the regular expression I'm using is not correct for checking 2 words with a space... the error I keep getting (and can't figure out) reads: "$("input#name").search is not a function".

There may be other issues as well but the above is what I'm stumbling on right now.

If anyone can assist me, I'd be HUGELY grateful!

Here's the code:

<script type="text/javascript">
$(document).ready(function(){
//Check Full Name Field
$('input#name').blur(function() {checkName(); });

});

function checkName()
{
var firstLast=/(\b[A-Za-z]+\b.*){2,}/

if($('input#name').search(firstLast)){
$('input#name').css('background-color','#FFCCCC');
$('span#eName').css('visibility','visible');
//return false;
}
else {
$('input#name').css('background-color','#E4FFCA');
$('span#eName').css('visibility','hidden');
//return true;
}
}
</script>

Fotiman

4:57 pm on Apr 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$('input#name')

That will return a jQuery object that containing the input element with id 'name'. Looks like you're expecting a string? You probably want the value of the input element, so try this:

if ($('input#name').val().search(firstLast)) {

neophyte

10:56 am on Apr 8, 2011 (gmt 0)

10+ Year Member



Fotiman -

Worked like a charm! Thanks so much.

Have a follow on question regarding the syntax for determining the value of a select list option - will post under a different message.

Again, thanks so much for your guidance!