Hi:
My form asks the user the following question:
If you have a "Discount Code" enter it below.
Most people will not have a code so the field cannot be required, but when it is filled, I want the jquery validation to guard me against malicious code.
The form works great. The field is checking for "alpha numeric values" and "minimum characters" but I cannot submit it unless the field is filled out.
I want the form to validate if and when the field is filled out only.
THIS IS THE HTML:
THIS IS THE JAVASCRIPT: (jquery)
Thanx beforehand
VirtualWeb
My form asks the user the following question:
If you have a "Discount Code" enter it below.
Most people will not have a code so the field cannot be required, but when it is filled, I want the jquery validation to guard me against malicious code.
The form works great. The field is checking for "alpha numeric values" and "minimum characters" but I cannot submit it unless the field is filled out.
I want the form to validate if and when the field is filled out only.
THIS IS THE HTML:
Code:
<div class="form-group"> <div class="form-group"> <label class="col-sm-3 control-label">Enter Code<span class="asterisk">*</span></label> <div class="col-sm-7"> <input type="text" name="Discount_Code" class="form-control popovers" placeholder="Enter Code"> </div> </div><!-- /.form-group -->
Code:
<script type="text/javascript">
var BlankonFormValidation = function () {
return {
// ====================
// CONSTRUCTOR APP
// ====================
init: function () {
BlankonFormValidation.jqueryValidation();
},
// ====================
// JQUERY VALIDATION
// ====================
jqueryValidation: function () {
// START TEXTATERA VALIDATION //
if($('#sample-validation-2').length){
$('#sample-validation-2').validate({
rules:{
Discount_Code:{
nosplchars:true,
minlength: 5
}
},
messages: {
Discount_Code: {
nosplchars: "no special characters",
minlength: jQuery.validator.format("Enter at least {0} characters")
}
},
highlight:function(element) {
$(element).parents('.form-group').addClass('has-error has-feedback');
},
unhighlight: function(element) {
$(element).parents('.form-group').removeClass('has-error');
}
});
}
// START SPECIAL CHARS REGEX
$.validator.addMethod("nosplchars", function(value, element) {
return /^[a-zA-z0-9\s]+$/.test(value);
}, "Special characters are not allowed");
// END SPECIAL CHARS REGEX
}
};
}();
// END TEXTATERA VALIDATION //
// Call main app init
BlankonFormValidation.init();
</script>
VirtualWeb