I have a registration form, in which I have used JavaScript form validator
My form
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<form class="form-group" method="post" id="contact-form">
{% csrf_token %}
<input type"text" name="username" placeholder="userid" id="id_username"/>
<input type"email" name="email" placeholder="email" id="id_email"/>
<input type"password" name="password1" placeholder="password1" id="id_password1"/>
<input type"password" name="password2" placeholder="password2" id="id_password2"/>
<input type="address" name="address" placeholder="address" id="id_address"/>
<input type="submit"/>
</from>
JavaScript
<script type="text/javascript">
$(document).ready(function () {
$('#contact-form').validate({
rules: {
username: {
minlength: 5,
required: true
},
email: {
required: true,
email: true
},
password1: {
minlength: 6,
required: true
},
password2: {
required: true,
minlength: 6,
equalTo: "#password1",
},
address: {
minlength: 5,
required: true,
},
},
highlight: function (element) {
$(element).closest('.control-group').removeClass('success').addClass('error');
},
success: function (element) {
element.text('OK!').addClass('valid')
.closest('.control-group').removeClass('error').addClass('success');
}
});
});
I want to RegEx pattern to address like only /-()., these charters are only allowed with alphabets
how do I use pattern RegEx in this?
try this
address: {
minlength: 5,
maxlenght=50,
required: true,
regex: /^[a-zA-Z0-9\s,'-./()]*$/,
},
Related
I have a signup form, and I am using a 3rd party library to validate the inputs. This library provides very nice and cool effects for validation, but I need to do another verification to make sure the password and confirm password are matching. how can I do that via jQuery? Here is my code:
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" />
</div>
<div class="form-group">
<label>Retype Password</label>
<input type="password" class="form-control" name="password2" />
</div>
and jQuery part:
$('#registrationForm').formValidation({
framework: 'bootstrap',
fields: {
password: {
validators: {
notEmpty: {
message: 'The password is required'
}
}
},
password2: {
validators: {
notEmpty: {
message: 'Password conformation is required'
},
equalTo: {
field: 'password',
message: 'The password cannot be the same as username'
}
}
}
}
});
What should I put instead of the question marks, to address my issue? Or maybe I am completely wrong with the syntax?
Working Fiddle
Html :
<form id="formCheckPassword">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" id="password" />
</div>
<div class="form-group">
<label>Retype Password</label>
<input type="password" class="form-control" name="cfmPassword" id="cfmPassword" />
</div>
<input type="submit" value="submit" />
</form>
and Jquery Rules :
$("#formCheckPassword").validate({
rules: {
password: {
required: true,
minlength: 6,
maxlength: 10,
},
cfmPassword: {
equalTo: "#password",
minlength: 6,
maxlength: 10
}
},
messages: {
password: {
required: "the password is required"
}
}
});
Update as per request :
$('#RegistrationForm').formValidation({
framework: 'bootstrap',
fields: {
password: {
validators: {
identical: {
field: 'confirmPassword',
message: 'The password and its confirm are not the same'
}
}
},
confirmPassword: {
validators: {
identical: {
field: 'password',
message: 'The password and its confirm are not the same'
}
}
}
}
});
I don't see it working in that syntax. If you want to do it via jQuery (as you mentioned), you can simply check the values:
var password1 = $('input[name=password]').val();
var password2 = $('input[name=password2]').val();
var match = password1 == password2
I've set all rules correctly, yet they seem to be ignored completely when I submit the Form.
Here is my code:
$(document).ready(function() {
$('#formValidation').validate({
rules: {
name: {
required: true,
minlength: 2,
lettersonly: true,
},
email: {
required: true,
minlength: 5,
email: true,
},
feedback: {
required: true,
minWords: 5,
},
}
messages: {
name: {
required: "Please enter your name",
},
},
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script>
<form id="formValidation" onload="validate()">
<p>Name: <br/><input type="text" name="name" /> </p>
<p>Email:<br/><input type="email" name="email" /></p>
<p>Feedback:<br/><input type="text" name="feedback" id="textfield" />
<input type="submit" id="submit" onsubmit="return formValidation()" /> </p>
</form>
So, all you need is the following.
$(document).ready(function () {
$('#formValidation').validate({
rules: {
name: {
required: true,
minlength: 2,
lettersonly:true,
},
email: {
required: true,
minlength: 5,
email:true,
},
feedback: {
required: true,
minWords: 5,
}
},
messages: {
name:{
required:"Please enter your name",
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script>
<form id="formValidation" method="post">
<p>Name: <br/><input type="text" name="name" /> </p>
<p>Email:<br/><input type="email" name="email" /></p>
<p>Feedback:<br/><input type="text" name="feedback" id="textfield"/>
<input type="submit" id="submit"/> </p>
</form>
Removed unnecessary/extra ,s from your script.
Remove inline event handlers
My imported scripts:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/additional-methods.min.js"></script>
My form:
<input name="tel" type="text" class="form-control contact" >
<input name="mobile" type="text" class="form-control contact" >
<input name="email" type="email" class="form-control contact" >
My validation rules:
$('#new-contact-form').validate({
rules: {
name: { required: true },
title: { required: true },
mobile: { require_from_group: [1, '.contact'] },
tel: { require_from_group: [1, '.contact'] },
email: { require_from_group: [1, '.contact'] },
}
});
When validator validates form inputs Im getting error message for each input: Please fill at least 1 of these fields even if I have one or more inputs filled. Did I missed something?
The validation is mot working, please specify the answer.
there is no validations working, the form is submitting without validations,
I am using the xajax in codeigniter
this is my view or form
<form name="frmcontact" id="frmcontact" class="contact-form" action="">
<input type="text" name="name" id="name" placeholder="Name">
<input type="text" name="email" id="email" placeholder="E-mail">
<input type="text" name="mobile" id="mobile" placeholder="Mobile">
<textarea class="message1" placeholder="Message" name="message" id="message"></textarea>
<span class="error"> </span>
<input type="submit" value="Submit" >
</form>
and my javascript is
<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery.validate.js"></script>
<script type="text/javascript">
;(function($) {
$(document).ready(function() {
$("#frmcontact").submit(function(e){
e.preventDefault();
}).validate({
rules: {
name: {
required: true,
number:false
},
email: {
required: true,
email:true
},
mobile: {
required: true,
number:true,
minlength:10,
maxlength:10
},
message: {
required: true
}
},errorElement: "span",
messages: {
},
errorPlacement: function(error, element) {
error.insertAfter(element);
},
submitHandler: function(){
xajax_contactsubmit(xajax.getFormValues('frmcontact'));
}
});
});
})(jQuery);
</script>
try this
jQuery(document).ready(function($) {
$("#frmcontact").validate({
rules: {
name: {
required: true,
number:false
},
email: {
required: true,
email:true
},
mobile: {
required: true,
number:true,
minlength:10,
maxlength:10
},
message: {
required: true
}
},errorElement: "span",
messages: {
},
errorPlacement: function(error, element) {
error.insertAfter(element);
},
submitHandler: function(){
xajax_contactsubmit(xajax.getFormValues('frmcontact'));
}
});
});
no need to handle and stop submit, the validate plugin will manage it by it self
I am very new to Jquery and hope you guys can help me with this jquery validation problem.
Been trying to validate the form but it does not validate at all. It accepts anything that I type in the field, regardless of what restrictions I set.
Please help. Thank you.
Here is my code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">
$(function(){
$('#form').validate({
alert("bbbb");
rules: {
name: {
required: true,
minlength: 2,
maxlength: 20,
lettersonly: true
},
ssn: {
required: true,
minlength: 9,
maxlength: 9,
nowhitespace: true
},
gender: {
required: true
},
mobile: {
required: true,
minlength: 10,
maxlength: 13,
digits: true
},
address: {
required: true,
minlength: 10,
},
email: {
required: true,
minlength: 6,
email: true
}
},
messages: {
name: {
required: "Please enter your name",
minlength: "Name should be more than 2 characters",
maxlength: "Name should be less than 20 characters",
lettersonly: "Name should contain only letters"
},
ssn: {
required: "Please enter your NRIC",
minlength: "NRIC should be more than 9 characters",
maxlength: "NRIC should be less than 9 characters",
nowhitespace: "NRIC should not have any spaces"
},
gender: {
required: "Please select your gender",
},
mobile: {
required: "Please enter your mobile number",
minlength: "Mobile number should be more than 10 characters",
maxlength: "Mobile number should be less than 13 characters",
digits: "Mobile number should contain only digits"
},
address: {
required: "Please enter your address",
minlength: "Address should be more than 10 characters",
},
email: {
required: "Please enter your email address",
minlength: "Password should be more than 6 characters",
email: "Please enter a valid email address"
}
},
});
$("#submit").click(function(){
$("#form").submit();
return false;
});
});
</script>
<div id="form">
<center>
<form id="form">
<div class="col-xs-12">
<input type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="col-xs-12">
<input type="text" name="nric" id="nric" placeholder="NRIC" required />
</div>
<div class="col-xs-12">
<select class="dropdown" id="gender" onChange="changeColor(this)">
<option value="" disabled selected>Gender</option>
<option value="female">Female</option>
<option value="male">Male</option>
</select>
</div>
<div class="col-xs-12">
<input type="text" name="mobile" id="mobile" placeholder="Mobile" required />
</div>
<div class="col-xs-12">
<input type="text" name="address" id="address" placeholder="Address" required />
</div>
<div class="col-xs-12">
<input type="email" name="email" id="email" placeholder="Email" required />
</div>
<input id="submit" class="button" type="submit" value="submit"/>
</form>
</center>
</div>
You have 2 elements with the id="form", so the validator is assigned to the div not to the form.
Change the id value of the div to something else.
Also the additional-methods.js file must be added after query.validate.js and there is a syntax error because of the alert() within the validate so
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script>
<div>
<center>
<form id="form">
<div class="col-xs-12">
<input type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="col-xs-12">
<input type="text" name="nric" id="nric" placeholder="NRIC" required />
</div>
<div class="col-xs-12">
<select class="dropdown" id="gender" onChange="changeColor(this)">
<option value="" disabled selected>Gender</option>
<option value="female">Female</option>
<option value="male">Male</option>
</select>
</div>
<div class="col-xs-12">
<input type="text" name="mobile" id="mobile" placeholder="Mobile" required />
</div>
<div class="col-xs-12">
<input type="text" name="address" id="address" placeholder="Address" required />
</div>
<div class="col-xs-12">
<input type="email" name="email" id="email" placeholder="Email" required />
</div>
<input id="submit" class="button" type="submit" value="submit"/>
</form>
</center>
</div>
then
jQuery(function ($) {
$('#form').validate({
rules: {
name: {
required: true,
minlength: 2,
maxlength: 20,
lettersonly: true
},
ssn: {
required: true,
minlength: 9,
maxlength: 9,
nowhitespace: true
},
gender: {
required: true
},
mobile: {
required: true,
minlength: 10,
maxlength: 13,
digits: true
},
address: {
required: true,
minlength: 10,
},
email: {
required: true,
minlength: 6,
email: true
}
},
messages: {
name: {
required: "Please enter your name",
minlength: "Name should be more than 2 characters",
maxlength: "Name should be less than 20 characters",
lettersonly: "Name should contain only letters"
},
ssn: {
required: "Please enter your NRIC",
minlength: "NRIC should be more than 9 characters",
maxlength: "NRIC should be less than 9 characters",
nowhitespace: "NRIC should not have any spaces"
},
gender: {
required: "Please select your gender",
},
mobile: {
required: "Please enter your mobile number",
minlength: "Mobile number should be more than 10 characters",
maxlength: "Mobile number should be less than 13 characters",
digits: "Mobile number should contain only digits"
},
address: {
required: "Please enter your address",
minlength: "Address should be more than 10 characters",
},
email: {
required: "Please enter your email address",
minlength: "Password should be more than 6 characters",
email: "Please enter a valid email address"
}
},
});
});
Demo: Fiddle
Hey you need to create custom rule in the library
like this
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;
}
and then have use it like this
rules: {
gender: {valueNotEquals: "default"}
},
messages: {
gender: { valueNotEquals: "Please select a payment method"}
}