/    Sign up×
Community /Pin to ProfileBookmark

How to get my prompt box to ignore string inputs? :S

Hi ppl, i’m new to this javascript situation, and am programming a simple website for the laugh… have 2 problems, the first is a prompt box situation where my code isnt supposed to read in letters, for example the user will be prompted to input how much money they want to lodge, if they add a string i get the NaN thing print out….

this is my code: (yes i no it’s very noobyish but my head is in bits atm lol)

function promptMsg()
{
lodge = prompt(‘how much would you like to lodge?’,0);

if (lodge == “A” || “B” || “C” || “D” || “E” || “F” || “G” || “H” || “I” || “J” || “K” || “L” || “M” || “N” || “O” || “P” || “Q” || “R” || “S” || “T” || “U” || “V” || “W” || “X” || “Y” || “Z”)
{
alert(“Error! Number input only!”);
}

//the method continues on here but thats working.
//this was supposed to sort out the NaN issue in case the user inputs a string however it doesnt, it ends up causing the error alert to pop up even when i input a number…. ne1 tell me wat i’m doing wrong and b able to give me some insight in how to get it to ignore letters and symbols and just accept numbers?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 17.2009 — [CODE]function promptMsg(){
var lodge;
while(isNaN(lodge)){
lodge= prompt('how much would you like to lodge?','0');
if(!lodge) lodge= '0';

lodge= parseFloat(lodge.replace(/[^d.]+/g,''))
}
return confirm('Is '+lodge+' correct?')? lodge: 0;
}[/CODE]
Copy linkTweet thisAlerts:
@AsterealauthorMar 19.2009 — cheers m8 ur a saviour, got that part workin now,

right my last problem is this:

when i have document.write() inside a function it prints to a different page...

so i dno how to sort that.... like inside the same function i have it print a balance, but instead of printing to the same page it basically goes to a blank page and prints the balance there... >.<

ne ideas? :S <3
Copy linkTweet thisAlerts:
@mintedjoMar 19.2009 — document.write only works if it's called while the page is loading, otherwise it loads a new one up.

If you need to add content to the page you can use the innerHTML property.

for example have an empty paragraph ready where you want the text to appear and set the contents when you get the value
[CODE]
html
<p id="myParagraph"></p>

javascript:
document.getElementById('myParagraph').innerHTML = myStringValue;
[/CODE]
Copy linkTweet thisAlerts:
@AsterealauthorMar 19.2009 — document.write only works if it's called while the page is loading, otherwise it loads a new one up.

If you need to add content to the page you can use the innerHTML property.

for example have an empty paragraph ready where you want the text to appear and set the contents when you get the value
[CODE]
html
<p id="myParagraph"></p>

javascript:
document.getElementById('myParagraph').innerHTML = myStringValue;
[/CODE]
[/QUOTE]


thanks loads m8, saviour, really appreciate it!
×

Success!

Help @Astereal 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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