/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Why does validation doesn’t work?

THis webpage calculates 2 numbers. However the validation won’t work. If user typed in space or letters in the textbox, an alert box should pop out saying that its an invalid entry. Please check my codes.. I really need your help guys..!!! Thanks

[QUOTE]

<HEAD>
<title>Radio</title>

<style type=”text/css”>
body
{
background-color:#B8BFD8;
}
</style>

<SCRIPT LANGUAGE=”JavaScript”>

function checkentry(a)
{
if(a.length>0 && Number(a)) {return true; }
else { alert(“invalid entry”); return false; }
}

function a_plus_b(form) {
a=eval(form.a.value)
b=eval(form.b.value)
c=a+b
form.ans.value = c
}
function a_minus_b(form) {
a=eval(form.a.value)
b=eval(form.b.value)
c=a-b
form.ans.value=c
}
function a_times_b(form) {
a=eval(form.a.value)
b=eval(form.b.value)
c=a*b
form.ans.value=c
}
function a_div_b(form) {
a=eval(form.a.value)
b=eval(form.b.value)
c=a/b
form.ans.value = c
}

</SCRIPT>

<BODY>
<form>
<table>
<tr>
<td><b>Enter first number: </b></td>
<td><input type=text size=4 name=”a” value=” ” onblur=”checkentry(this.value)”></td>
</tr>

<tr>
<td><b>Enter second number: </b></td>
<td><input type=text size=4 name=”b” value=” ” name=”b”></td>
</tr>

<tr>
<td><b>Addition</b></td>
<td><input type=radio value=” + ” name=”operator” onClick=”a_plus_b(this.form)”>**</td>
</tr>

<tr>
<td><b>Subtraction</b></td>
<td><input type=radio value=” – ” name=”operator” onClick=”a_minus_b(this.form)”> </td>
</tr>

<tr>
<td><b>Multiplication</b></td>
<td><input type=radio value=” x ” name=”operator” onClick=”a_times_b(this.form)”>**</td>
</tr>

<tr>
<td><b>Division</b></td>
<td><input type=radio value=” / ” name=”operator” onClick=”a_div_b(this.form)”>* </td>
</tr>*

<tr>
<td><b>Equals</b></td>
<td><input type “number” value=0 name=”ans” size=9></td>
</tr>

<tr>
<td></td>
<td><input type=”reset” value=”Reset!”></td>
</tr>

</table>

</FORM>
</body>
</html>

[/QUOTE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@jomawieauthorAug 15.2010 — Please help!!
Copy linkTweet thisAlerts:
@FangAug 15.2010 — function checkentry is not used in the 2nd entry.

Don't use eval, use parseInt or parseFloat
Copy linkTweet thisAlerts:
@jomawieauthorAug 15.2010 — thank you very much.. everything's fix now..!
×

Success!

Help @jomawie 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,
)...