Validating an Optional field with Bootstrap Jquey

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • virtualweb
    New Member
    • Aug 2007
    • 30

    Validating an Optional field with Bootstrap Jquey

    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:

    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 -->
    THIS IS THE JAVASCRIPT: (jquery)

    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>
    Thanx beforehand
    VirtualWeb
    Attached Files
    Last edited by virtualweb; Jun 9 '16, 03:55 PM. Reason: Adding the code to the post instead of in separate file
Working...