Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

jquery/javascript how does one do a string check and numeric check (either or)

i have a search box im doing id like to fire it off with more then 3 char and to fire off if its any number for search.
i can do it fine if i have just the >3 works fine i cant get it to check if its a number and fire.
how does one do this please
here my code
var faq_search_input = $(this).val();
var dataString = 'keyword='+ faq_search_input;
if(faq_search_input.length>3 || isNaN(faq_search_input))

Open in new window


thank you in advance for any code or help you may provide.
Johnny
ASKER CERTIFIED SOLUTION
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

im using

Open in new window

jquery see full below code for that section.

$("#faq_search_input").keyup(function()
{
var faq_search_input = $(this).val();
var dataString = 'keyword='+ faq_search_input;
if(faq_search_input.length>3 || isNaN(faq_search_input))
{
$.ajax({
type: "GET",
url: "ajax-search.php",
data: dataString,
beforeSend:  function() {

$('input#faq_search_input').addClass('loading');

},
success: function(server_response)
{

$('#searchresultdata').html(server_response).show();
$('span#faq_category_title').html(faq_search_input);

if ($('input#faq_search_input').hasClass("loading")) {
 $("input#faq_search_input").removeClass("loading");
        }

}
});
}return false;
});

Open in new window


here is where i got the code from and im trying to mod it to fit my needs of searching a string in the mysql database of both text and numeric values.
http://youhack.me/2010/04/28/creating-a-fancy-search-feature-with-php-mysql-and-jquery/
i have my search working, but again im trying to mod the limiting features to fire the event on just one number for the search. as i have it looking for a field that only has numbers as well as the item text search field too (like an item code field and a item name field searches)
thanks for the quick reply
Avatar of Johnny

ASKER

so would this be correct then?
if(faq_search_input.length>3 || parseInt(isNaN(faq_search_input)))

Open in new window

no, you parse the input first and then check to see if that ended up as a number after so it would be isNaN(parseInt(faq_search_input))
Avatar of Johnny

ASKER

this seams to fit my needs a bit t=better looking if the string is a number or not
if(isNaN(parseFloat(faq_search_input)) || faq_search_input.length>3)

Open in new window


but both ways yours and this one breaks the 3 chars needed for a text alpha string.

as i said im trying to fire it off if its a item number or an item name. limiting the item name to 3 or more chars for search(the default in the code i found) and just 1 number if fires search for item number..

still not working corectly im not sure why not too
Avatar of Johnny

ASKER

got it i was missing an ! for not

if(!isNaN(parseInt(faq_search_input)) || faq_search_input.length>3)

Open in new window

Avatar of Johnny

ASKER

thank you