PDA

View Full Version : Using javascript to submit a SPRY validated form



tanel96
March 22nd, 2008, 11:09 AM
I have a basic html form and some spry validation to check for proper email addresses and phone numbers. Everything works fine when i try to submit the form with a "standard" button like this:



<input type="submit" name="button" id="button" value="Submit" />


but when i use something like this:



<script language="javascript" type="text/javascript">
function submitForm()
{
document.forms['myForm'].submit();
}
</script>
<input type="button2" name="Submit" id="button2" value="Submit" onclick="submitForm();" />


Then it submits the form BUT doesn't check for validation any more... why is that ?

actionAction
March 22nd, 2008, 07:59 PM
From Adobe Labs http://labs.adobe.com/technologies/spry/articles/checkbox_overview/index.html:

Specify when validation occurs

By default, the Validation Checkbox widget validates when the user clicks the submit button. You can, however, set two other options: blur or change. The validateOn:["blur"] parameter causes the widget to validate whenever the user clicks outside the widget. The validateOn:["change"] parameter causes the widget to validate as the user makes selections.

To specify when validation occurs, add a validateOn parameter to the constructor as follows:

<script type="text/javascript">
var sprycheckbox1 = new Spry.Widget.ValidationCheckbox("sprycheckbox1", {validateOn:["blur"]});
</script>

As a convenience, you can discard the brackets if your validateOn parameter contains a single value (for example, validateOn: "blur"). If the parameter contains both values, however (validateOn:["blur", "change"]), include brackets in the syntax.

tanel96
March 23rd, 2008, 03:21 PM
unfortunately that doesn't help me much, because i need it to validate when i submit the form via my javascript -> document.forms['myForm'].submit();

it might be a bit difficult to understand, so i added two examples, i hope it clears up the situation

http://fritz.planet.ee/test/validation/example1.html <- OK
http://fritz.planet.ee/test/validation/example2.html <- NOT OK

Grofit
March 26th, 2008, 10:14 AM
can you not just put an onSubmit(return checkValidation();) event on your form so when it is submitted it will check, if you return a false your form will not be submitted...

borrob
March 27th, 2008, 05:15 AM
you should change:

document.forms['myForm'].submit();

to:

myForm.submit();

thanksgiver
February 25th, 2009, 01:15 PM
I have a basic html form and some spry validation to check for proper email addresses and phone numbers. Everything works fine when i try to submit the form with a "standard" button like this:



<input type="submit" name="button" id="button" value="Submit" />


but when i use something like this:



<script language="javascript" type="text/javascript">
function submitForm()
{
document.forms['myForm'].submit();
}
</script>
<input type="button2" name="Submit" id="button2" value="Submit" onclick="submitForm();" />


Then it submits the form BUT doesn't check for validation any more... why is that ?

----------------------------Right Way----------------------------



function validateonsubmit(){
var myForm=document.getElementById("form1");
var SS= Spry.Widget.Form.validate(myForm);
if(SS==true){
myForm.submit();
}
}




<a href="#" onclick="javascript:validateonsubmit();">Validate on Submit</a>

philzy127
March 22nd, 2012, 03:49 PM
@thanksgiver: This has worked flawlessly, thank you very much!
:hugegrin: