[SOLVED] using isnan()

This is a discussion on "[SOLVED] using isnan()" within the JavaScript Forum section. This forum, and the thread "[SOLVED] using isnan() are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 25th, 2007, 11:49
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Question [SOLVED] using isnan()

Hi,
so yet more problems for me with javascript!!

What I am attempting to do is create a phone number validator which checks;

1.If there is nothing entered into the field return with an alert.

2.If there are any spaces within the text entered into the field, delete them.

3.After the spaces have been deleted check if the entered data is a number using isnan(), and check that the number is no shorter than 11 digits. Then return true! - if the number is shorter than 11 digits return with a alert message.

Number 1 isnt a problem, but to be honest I dont have clue how to do numbers 2 and 3. Anyone had any experience doing this before and if so how should i fix it??

Code: Select all
<script type="text/javascript">
<!--

function validate_form ( )
{

    if (document.callmeback_form.name.value == "" )
    {
    alert ( "Please enter your name." );
    document.callmeback_form.name.focus();
    document.callmeback_form.name.style.background = '#FF8600';
    document.callmeback_form.name.style.border= "1px solid #A5ACB2";
    document.callmeback_form.name.style.width= "132px";
    document.callmeback_form.name.style.height= "15px";
    return false;
    }
    
    if (document.callmeback_form.telephone.value == "" )
    {
    alert ( "Please enter your number (inc. area code)." );
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border= "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width= "132px";
    document.callmeback_form.telephone.style.height= "15px";
    return false;
    }
    
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
    //strip out acceptable non-numeric characters
    if (document.callmeback_form.telephone(isNaN(parseInt(stripped)))) 
    {
    error = "The phone number contains illegal characters.";
    }

}
//-->
</script>        
<form class="register" action="callmeback-action.php" method="post" name="callmeback_form" onsubmit="return validate_form ( );"><input type="hidden" name="previous" value="<?PHP echo $_SERVER["HTTP_REFERER"]; ?>" />
<div id="regtype">
<div><label for="radnow">Call me NOW:</label><input type="radio" name="when" id="radnow" value="now" class="radio" checked="checked" onclick="regenable();" /></div>
<div><label for="rad30min">Call me in 30 Minutes:</label><input type="radio" name="when" id="rad30min" value="30min" class="radio" onclick="regenable();" /></div>
<div><label for="radtime">Call me at a specific time:</label><input type="radio" name="when" id="radtime" value="time" class="radio" onclick="regenable();" /></div>
</div>

<div>
<div><label for="name" id="namelabel">Name:</label><input name="name" id="name" type="text" class="text" /></div>
<div><label for="telephone" id="telephonelabel">Telephone No:</label><input name="telephone" id="telephone" type="text" class="text" /></div>

Last edited by eon201; Oct 25th, 2007 at 11:53.
Reply With Quote

  #2 (permalink)  
Old Oct 25th, 2007, 12:09
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: using isnan()

Okay, you need to use isNaN, note the capitilisation to check this and normally you need to try get a number out of it.

You could try this simple code

Code: Select all
if (document.callmeback_form.telephone.value == "" || isNaN(parseInt(document.callmeback_form.telephone.value)))
    {
    alert ( "Please enter your number (inc. area code)." );
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border= "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width= "132px";
    document.callmeback_form.telephone.style.height= "15px";
    return false;
} else {
	document.callmeback_form.telephone.value = document.callmeback_form.telephone.value.replace(/\s/g, ""); // strip whitespace
}
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Oct 25th, 2007, 12:20
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: using isnan()

wow!

Thats excellent. I am indebted to you.

So basically the script now checks if there is any data entered into the field, if not it raises an alert. And if there is any whitespace it is deleted.

From this point how can i get the javascript to check that there are 11 digits after deleting the whitespace, and if false raise another alert along the lines of "The number you have entered is to short or to long, please check and try again".??

ps. you've probably noticed im a noob by now!
Reply With Quote
  #4 (permalink)  
Old Oct 25th, 2007, 12:24
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: using isnan()

oh. Also where you have used the replace command
document.callmeback_form.telephone.value.replace(/\s/g, "")

is there a list on the web of commands to delete certain character types?? eg.letters, or numbers or whitespaces??

Thanks again. Dan.
Reply With Quote
  #5 (permalink)  
Old Oct 25th, 2007, 12:24
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: using isnan()

LOL... Everyone's a noon once

You can check the length of a string with .length see below
Code: Select all
document.callmeback_form.telephone.value = document.callmeback_form.telephone.value.replace(/\s/g, ""); // strip whitespace 1st


if (document.callmeback_form.telephone.value == "" || isNaN(parseInt(document.callmeback_form.telephone.value)))
    {
    alert ( "Please enter your number (inc. area code)." );
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border= "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width= "132px";
    document.callmeback_form.telephone.style.height= "15px";
    return false;
} else if (document.callmeback_form.telephone.value.length < 11)
    alert ('Don\'t try and short change the numbers');
// .length is the length of a string so checking if it's less than 11 characters will help you there
	}

PS. As for the regular expressions (which is that string you refer to) there are many many many uses for this.

Do a google search for Perl Compatible Regex and you will see they are widely discussed and used.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Oct 25th, 2007 at 12:27.
Reply With Quote
  #6 (permalink)  
Old Oct 25th, 2007, 12:38
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: using isnan()

Thanks Rakuli!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] 1 solved problem causes another delusion Web Page Design 9 Dec 21st, 2007 08:12
Adding "Topic Solved" for solved topics AdRock Webforumz Suggestions and Feedback 21 Oct 4th, 2007 15:08


All times are GMT. The time now is 10:41.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43