Click to See Complete Forum and Search --> : multiple scripts


rbollen
04-30-2003, 06:08 AM
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>

Charles
04-30-2003, 06:23 AM
Your function checkDecimals 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:


<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>

rbollen
04-30-2003, 07:10 AM
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 :D

Charles
04-30-2003, 11:15 AM
Make that button a Submit button and then validate "onsubmit" like I did in my example.