I'm using jQuery Validation Plugin and my code is not working as intended. What I want is when I click the submit button the program will check if the form is properly filled out and contains a url and then runs a function. In this case it always alert('true
).
$(document).ready(function(){
$('#button1').submit(function() {
if($("#button1").valid()){
alert('true');
}
else{
alert('false');
}
});
});
Not sure if this is relevant but I'm using Flask and WTForms:
Form:
class SVN_Path(Form):
svn_url1=StringField('SVN Directory:',[validators.URL()])
svn_url2=StringField('SVN Directory:',[validators.URL()])
Html:
<form id="button1" action="{{ url_for('test4') }}"method="post" name="SVNPATHS">
{{form1.hidden_tag()}}
<br>
<div class="svninput" id="input1">
{{ form1.svn_url1.label }}
{{ form1.svn_url1 }}
</div>
<input id="update" class="button" type="submit" name="updatepaths" value="Update">
<div class="svninput" id="input2">
{{ form1.svn_url2.label }}
{{ form1.svn_url2 }}
</div>
</form>
UPDATE
$('#button1').submit(function(){
var is_valid = $(this).validate({
rules: {
svn_url1: {
required: true,
url: true
},
svn_url2: {
required: true,
url: true
}
}
});
if(is_valid){
alert('true');
}
else{
alert('false');
}
});
});
I tried what dirn had suggested but this still gives back true even if both fields are empty.
You could try it this way:
This will validate the form when the button is clicked, instead of when the form is clicked.