/    Sign up×
Community /Pin to ProfileBookmark

multiple scripts

I’ve got a problem

i want to use both these javascripts, but i can’t make it work.

i have 1 button on my form, and when i press it, i have to check if the input box is empty and if the value is a number

can anyone help me

the first one is?to check if its is a decimal)

<SCRIPT LANGUAGE=”JavaScript”>

function checkDecimals(fieldName, fieldValue) {

decallowed = 0; // how many decimals are allowed?

if (isNaN(fieldValue) || fieldValue == “”) {
alert(“Oops! That does not appear to be a valid number. Please try again.”);
fieldName.select();
fieldName.focus();
}else {
if (fieldValue.indexOf(‘.’) == -1) fieldValue += “.”;
dectext = fieldValue.substring(fieldValue.indexOf(‘.’)+1, fieldValue.length);

if (dectext.length > decallowed){
alert (“Oops! Please enter a number with up to ” + decallowed + ” decimal places. Please try again.”);
fieldName.select();
fieldName.focus();
}
}
}

</script>

with this form:
<form action=”test.html”>
Please enter a number with up to 2 decimal places: <br>
<input type=text name=”aantalbest”>
<input type=button name=ok value=”Ok” onClick=”checkDecimals(this.form.aantalbest, this.form.aantalbest.value)”>
</form>

and this script(to check if ipnut is not empty)

<Script lanaguage=”javascript”>
function Valideren(aform)
{
if (aform.aantalbest.value == “”)
{
alert(“Geef een aantal door aub…”);
aform.aantalbest.focus();
return(false);
}
return(true);
}
</script>

with this form
<FORM action=”test2.html” method=”POST” onSubmit=”return Valideren(this)”>
<INPUT Name=”aantalbest” size=”50″>
<INPUT type=”SUBMIT” value=” Toevoegen “>
</FORM>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CharlesApr 30.2003 — [font=georgia]Your function [font=monospace]checkDecimals[/font] already checks if the field is not a number and if the field is empty. The part about checking the number of decimals will not work too well but you write that you are not worried about that. To clean up what you have:[/font]

[font=monospace]

<script type="text/javascript">

<!--

function validate(control) {

if (isNaN(control.value) || control.value == '') {

alert('That would not appear to be a valid entry.');

this.value='';

this.focus();

return false;

}

}

// -->

</script>


<FORM action="test2.html" method="POST" onsubmit="return validate(this.aantalbest)">

<INPUT Name="aantalbest" size="50">

<INPUT type="SUBMIT" value=" Toevoegen ">

</FORM>[/font]
Copy linkTweet thisAlerts:
@rbollenauthorApr 30.2003 — The code above is not perfect yet

i've changed into this one:

<html>

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function checkDecimals(fieldName, fieldValue) {

if (isNaN(fieldValue)) {

alert("Oops! That does not appear to be a valid number. Please try again.");

fieldName.select();

fieldName.focus();

}

if(fieldValue == "") {

alert("Oops! The input box is empty. Please try again.");

fieldName.select();

fieldName.focus();

}

}

</script>

</HEAD>

<BODY>

<center>

<form action="test.html" method="post" >

Please enter a number with up to 2 decimal places: <br>

<input type=text name="aantalbest">

<input type=button name=ok value="Ok" onClick="return checkDecimals(this.form.aantalbest, this.form.aantalbest.value)" >

</form>

</center>

</body>

</html>

but now I have an other problem.

When I click on button ok, it works

but how can I also make it work when I press enter??

is there a method like onEnter or something?

oh, bye the way thankx charles for the good advice ?
Copy linkTweet thisAlerts:
@CharlesApr 30.2003 — [font=georgia]Make that button a Submit button and then validate "onsubmit" like I did in my example.[/font]
×

Success!

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