/    Sign up×
Community /Pin to ProfileBookmark

Is it a number?

Can anyone tell me how to test whether

event.srcElement.value

is a number?

Thanks for any help.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@BillyRayJul 28.2004 — if (!isNaN(event.srcElement.value)) alert('It\'s a number!');

Hope this helps,

Dan
Copy linkTweet thisAlerts:
@WebskaterauthorJul 28.2004 — Thanks for your answer.
Copy linkTweet thisAlerts:
@steelersfan88Jul 28.2004 — Webskater ... remember never to forget using [font=monospace]document.[/font] when referencing a form element.
Copy linkTweet thisAlerts:
@compuboy1010Oct 27.2004 — What about this function? It doesn't work:

function checkBirtdate(form) {

if (!form.birthday.value) || !form.birthmonth.value || !form.birthyear.value) {

alert("Please enter your birthdate.");

form.birthday.focus();

return false;

} else if (isNaN(form.birthday.value) || isNaN(form.birthmonth.value ) || isNaN(form.birthday.value)){

alert("Please enter a valid birthdate Format TT/MM/JJJJ ein ?");

form.birthday.focus();

return false;

} else {

return true;

}

}
Copy linkTweet thisAlerts:
@baconbuttyOct 27.2004 — Some thoughts:


if (!form.birthday.value) || !form.birthmonth.value || !form.birthyear.value)

[B]LOOKS LIKE AN EXTRA BRACKET ON THE FIRST CONDITION[/B]

{

alert("Please enter your birthdate.");

form.birthday.focus();

return false;
[/QUOTE]



if (isNaN(form.birthday.value) || isNaN(form.birthmonth.value ) || isNaN(form.birthday.value))
[/QUOTE]


As I understand it "NaN" is a special value in Javascript, and isNaN tests for that value. I don't think you can use it generally (others, I am sure you will correct me if I am wrong). You need to do something to obtain it. Thus try the following:-

[CODE]

if (isNaN(parseInt(form.birthday.value)) || isNaN(parseInt(form.birthmonth.value)) || isNaN(parseInt(form.birthday.value))

[/CODE]


Of course the above offers only limited validation, and you may wish to explore using regular expressions to validate your form.
×

Success!

Help @Webskater 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.15,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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