I have multiple forms in my web, you can show/hide them depeding on your query, but the jquery validate just work on the first one (Particular).
Here I have my forms: https://www.lokkura.es/contacto2.php clicking on the left panel you will change the form.
Form 1 (jquery validation works here):
<div id="form1">
<form id="form" class="form" action="#" method="post">
<div class="clearfix">
<div class="col-md-6 text-center">
<div class="control-group">
<input name="name" type="text" class="validate['required'] textbox1" placeholder="* Nombre : "
onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Nombre :'" />
</div>
<div class="control-group">
<input name="name" type="text" class="validate['required'] textbox1" placeholder=" Ciudad : "
onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Ciudad :'" />
</div>
<div class="control-group">
<input name="email" type="text" class="validate['required','email'] textbox1"
placeholder="* Email : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Email :'"/>
</div>
<div class="control-group">
<input name="phone" type="text" class="validate['required'] textbox1"
placeholder="* Teléfono : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Teléfono :'"/>
</div>
</div>
<div class="col-md-6 text-center">
<div class="control-group">
<textarea name="message" class="validate['required'] messagebox1"
placeholder="* Mensaje : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Mensaje :'"></textarea>
</div>
</div>
<div class="clearfix">
<input value="Enviar Mensaje" name="submit" type="submit" class="submitBtn" />
</div>
<div id="post-message-contact" class="post_message"></div>
</div>
</form>
</div>
Form 2 (jquery validation does not work here):
<div id="form2" style="display:none">
<form id="form" class="form" action="#" method="post">
<div class="clearfix">
<div class="col-md-6 text-center">
<div class="control-group">
<input name="name" type="text" class="validate['required'] textbox1" placeholder="* Nombre : "
onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Nombre :'" />
</div>
<div class="control-group">
<input name="name" type="text" class="validate['required'] textbox1" placeholder=" Ciudad : "
onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Ciudad :'" />
</div>
<div class="control-group">
<input name="email" type="text" class="validate['required','email'] textbox1"
placeholder="* Email : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Email :'"/>
</div>
<div class="control-group">
<input name="name" type="text" class="validate['required'] textbox1" placeholder=" Empresa : "
onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Empresa :'" />
</div>
<div class="control-group">
<input name="name" type="text" class="validate['required'] textbox1" placeholder=" Web de Empresa : "
onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Web de Empresa :'" />
</div>
<div class="control-group">
<input name="phone" type="text" class="validate['required'] textbox1"
placeholder="* Teléfono : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Teléfono :'"/>
</div>
</div>
<div class="col-md-6 text-center">
<div class="control-group">
<textarea name="message" class="validate['required'] messagebox1"
placeholder="* Mensaje : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Mensaje :'"></textarea>
</div>
</div>
<div class="clearfix">
<input value="Enviar Mensaje" name="submit" type="submit" class="submitBtn" />
</div>
<div id="post-message-contact" class="post_message"></div>
</div>
</form>
</div>
Javascript Code:
jQuery(document).ready(function($){
"use strict";
$('#form').validate(
{
rules: {
name: {
minlength: 2,
required: true
},
phone: {
required: true,
},
email: {
required: true,
email: true
},
message: {
minlength: 2,
required: true
}
},
highlight: function(element) {
$(element).closest('.control-group').removeClass('success').addClass('error');
},
success: function(element) {
element
.text('').addClass('valid')
.closest('.control-group').removeClass('error').addClass('success');
},
submitHandler: function(form) {
// do other stuff for a valid form
$.post('contact_form.php', $("#form").serialize(), function(data) {
// action file is here
$('#post-message-contact').html(data);
});
}
});
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…