Forums

Using onChange and onSubmit javascript

stirfry213 19 Apr, 2009
Currently, I am using some javascript validation code for an INPUT as below:
function valuevalidation(entered, min, max, alertbox, datatype)
{
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

And I call it like this:
Percentage Interest <INPUT class=cf_inputbox id=text_3  maxLength=5 size=5 name=text_3 onChange="valuevalidation(this,0,100,'Interest must be between 100 and 0');">%


What I need help with is getting the same validation check to be done onSubmit BEFORE the page is sent. If validation fails, the form is not submitted. If at all possible, I would like to make sure it won't blow out the values that the user has already put into the form because my form has 24 fields.

Any suggestions would be great.
GreyHead 19 Apr, 2009
Hi strifry213,

If you have the latest RC4.11 ChronoForms release you should be able to use this in your form HTML:
<?php
$script = "
window.addEvent('domReady', function () {
  var text_3 = new LiveValidation('text_3');
  text_3.add( Validate.Numericality, { 
    minimum: 0, 
    maximum: 100,
    tooLowMessage: 'Must be at least 0',
    tooHighMessage: 'Must 100 or less',
  });
});
";
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration($script);
?>
. . .
Percentage Interest <input class='cf_inputbox' id='text_3' maxLength='5' size='5' name='text_3' />%
Not tested and may need debugging.

Bob
This topic is locked and no more replies can be posted.