1

following is my piece of code which checks if the input format is EMAIL which comes from the form input or not and validates accordingly i want to know how can i modify the following code that validates if the input was only number

 if(email.length == 0 || email.indexOf('@') == '-1'){ var error = true; $('#email_error').fadeIn(500); }else{ $('#email_error').fadeOut(500); } 
1
  • THANKS TO ALL FOR SUCH A QUICK RESPONSE!!!! Commented Apr 22, 2012 at 5:15

4 Answers 4

2

Use jQuery's IsNumeric method.

http://api.jquery.com/jQuery.isNumeric/

$.isNumeric("-10"); // true 
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer, but the OP doesn't need jQuery for this.
I know. Still, it's an extra function call when you could just go native.
I would normally agree with Elliot, but since jQuery is already used, isNumeric is close to native ;) I do not like to mix native methods, since you get horrible stuff like $(this).jqMethod({ this.nativeMethod()})
2
if (email.match(/[0-9]+/)) { //it's all numbers } 

EDIT As mentioned in the comments, to ensure that the entry is ALL numbers, the regex would have to include the begin and end characters, ^ and $:

if (email.match(/^[0-9]+$/)) { //it's all numbers } 

Or even more succinctly:

if (email.match(/^\d+$/)) { //it's all numbers } 

I don't deserve the credit for that fix, but I did want to correct it for anyone who may come find this later.

8 Comments

@ElliotBonneville I didn't proof it. Am I wrong? I'll delete if so.
No, it's right. Just... there are simpler methods of doing this. =) I generally like to avoid RegEx unless I really need it.
@ElliotBonneville Hmm... this one seems pretty simple to me, but I'm genuinely interested in why you think it's not. Is it inefficient?
I don't have any sources that say it's less efficient than isNan(Number(myStr));, but I have a hunch using RegEx will be slower. Plus, I'm biased against RegEx, so that factored into my comment as well.
Understood. I'm happy to take note (and get better) when I do things wrong. But thanks for not down voting a correct answer!
|
1

I would use:

if(isNaN(email*1)){ //evaluated to NaN }else{ //evaluated to number } 

In this case the (email*1) have the possibility to evaluate to NaN, and thus will fail the check because the list of falsish values are 0,"",false,null,undefined,NaN

13 Comments

No typos here. Very nice. Couldn't you also do if(+email)? Minor note/random thought: Huh, there are generally quite a few ways to check if a value is a number in JS.
You can't compare to null, so technically it's not a falsy value.
@ElliotBonneville null*1 evaluates to NaN and fail the if() check
Ah, you weren't clear on that before the edit. Now I see what you did there. =)
It looks like the most elegant solution, but if I have to choose between if(email*1){ //evaluate to number - which relies on side-effects and needs a comment and $.isNumeric(email) which does not, I would lean towards the isNumeric one...
|
1

Check if converting the email address to a Number object returns a 'Not a Number' value; if not, the input was a number. The code would look like this:

if(!isNaN(Number(email)) { 

5 Comments

Whoops, yep, typos will be the death of me. Nice catch.
@El indeed, you now wrote isNan :)
So the person I tend to agree with managed to create a monster of a native test to see if some text is a number or not. Please tell me what construct would not work if you did just isNaN instead of isNaN(Number(email))
@mplungjan: Not sure if this is an issue but see this answer: stackoverflow.com/a/6441835/339852
It ain't an issue in my browsers (osx Safari5 Chrome 18, Fx 12): jsfiddle.net/MWwBq

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.