/    Sign up×
Community /Pin to ProfileBookmark

numbers only text input.

hello.
i’m new to javascript, and trying to code something that seems easy, i got stuck.

what i want to do is:
if you enter anything except numbers in a form and you click this button “send” to send the info, a window will pop up saying “enter numbers only”.

this is the code i have:

function validatephone(){
if(document.form.phone.value!=’????’){
alert(“Enter numbers only.”);
return false;
}

is that the right way? if it is, what should i enter in ???? so it will accept numbers only?
if that’s wrong, what’s the right way?

i hope someone comes with help. thanks!

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@efervescenteauthorDec 09.2006 — ok, thanks, i'm gonna play around with those codes and try to get a good result.

still if anyone has the exact code, post it here!
Copy linkTweet thisAlerts:
@CharlesDec 09.2006 — <input onkeyup="this.value = this.value.replace (/D/, '')">
Copy linkTweet thisAlerts:
@KorDec 09.2006 — when you said about "phone" named element in a form, we all understood what's all about. Otherwise, be careful, when dealing with programming languages (and mathematics as well) you should specify which kind of numbers are? decimal? Other base? If decimal, are they real, integer, floated?...
Copy linkTweet thisAlerts:
@efervescenteauthorDec 09.2006 — i used phone as example beacuse... it has numbers. i don't want anything but numbers, from 0 to 9, if you enter 551 or 123 or 6312, it will be accepted. if you enter anything that is not a number, like 0.98 or $%gg, and alert will pop up when you click the "send" button that sends the info (i forgot this part in the first post) asking you to Please enter numbers only.
Copy linkTweet thisAlerts:
@CharlesDec 09.2006 — Do give my example above a try.
Copy linkTweet thisAlerts:
@konithomimoDec 09.2006 — <input onkeyup="this.value = this.value.replace (/D/, '')">[/QUOTE]

Using that wont work if the user holds down a key. It will only remove one instance of a non-numeric input
Copy linkTweet thisAlerts:
@CharlesDec 09.2006 — Oops. But this''l correct for that:<input onkeyup="this.value = this.value.replace (/D+/, '')">
Copy linkTweet thisAlerts:
@gooncorpDec 09.2006 — when the user submits the value run this code:[code=php]
if(parseInt(input.value)!=input.value){
alert('enter only numbers');
return false;
}[/code]

where input is the input box you are referring to.
Copy linkTweet thisAlerts:
@efervescenteauthorDec 09.2006 — Charles, i think that's not exactly what i want, i want numbers only, your code says something about replace.

gooncorp's code is the one i needed. thanks!

thanks everyone for helping!

i found this one http://simplythebest.net/scripts/DHTML_scripts/javascripts/javascript_25.html that is interesting, but if you paste something from clipboard that isn't a number, it will be accepted. so i'll just stick to my original idea (pop up).

thanks!
Copy linkTweet thisAlerts:
@CharlesDec 09.2006 — Charles, i think that's not exactly what i want, i want numbers only, your code says something about replace.[/QUOTE]Bother to try my example, exactly the way it is, just for kicks.
Copy linkTweet thisAlerts:
@ayse_incebulutDec 09.2006 —  <br/>
&lt;script type="text/javascript"&gt;

function onKeyPressNumbers(e)
{
var key = window.event ? e.keyCode : e.which;

// alert(key) ;

var keychar = String.fromCharCode(key);

// alert(keychar);

reg = /D/;


// alert(reg.test(keychar)) ;

// alert(!reg.test(keychar)) ;

return !reg.test(keychar);
}
&lt;/script&gt;

&lt;body&gt;
&lt;form&gt;
&lt;input type="text" onkeypress="return onKeyPressNumbers(event);" /&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@konithomimoDec 09.2006 — Charles, i think that's not exactly what i want, i want numbers only, your code says something about replace.

gooncorp's code is the one i needed. thanks!

thanks everyone for helping!

i found this one http://simplythebest.net/scripts/DHTML_scripts/javascripts/javascript_25.html that is interesting, but if you paste something from clipboard that isn't a number, it will be accepted. so i'll just stick to my original idea (pop up).

thanks![/QUOTE]

It would be best to use the regular expression that Charles suggested.

replace() is just a method that checks a string, character by character, against the first argument . . . in this case it checks to see if it is anything other than a number . . . and then replaces the specified character with the second argument . . . in this case '', which is two single quotes, thus meaning replace it with nothing . . . if it is not what you want, a number.
Copy linkTweet thisAlerts:
@g78Aug 21.2010 — Bother to try my example, exactly the way it is, just for kicks.[/QUOTE]

nifty code, i like it.

its simple and effective. ?
Copy linkTweet thisAlerts:
@OfekmeisterAug 21.2010 — [CODE]
<html>

<head>

<script type="text/javascript">
<!--

function ValidatePhone(validNumber)
{

validNumber = parseInt(validNumber);

if(isNaN(validNumber))
{

alert("Enter numbers only.");

return false;

}

else
{

return true;

}

}

function ValidateForm()
{

var validNumber = document.getElementsByName('phone');

if (!ValidatePhone(validNumber.value))
{

return false;

}

else
{

// put whatever you want javascript to do with the number

}

}

//-->
</script>

</head>

<body>

<form onsubmit="ValidateForm(); return false;"

Enter phone number: <input type="text" name="phone" maxlength="10" onchange="return ValidatePhone(this.value)" <br /> />

<input type="submit" value="Send" />

</form>

</body>

</html>
[/CODE]
×

Success!

Help @efervescente spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...