lundi 10 octobre 2016

Add Checkbox validation

Currently the date and year is being verified but I would like to add a checkbox to the list.

Form code:

var html = '';
    html += '<div class="ac-overlay"></div>';
    html += '<div class="ac-container">';
    html += '<h2>' + settings.title + '</h2>';
    html += '<p>' + copy.replace('[21]','<strong>'+settings.minAge+'</strong>'); + '</p>';
    html += '<div class="errors"></div>';
    html += '<div class="fields"><select class="month">';for(var i=0;i<months.length;i++){
    html += '<option value="'+i+'">'+months[i]+'</option>'}
    html += '</select>';
    html += '<input class="day" maxlength="2" placeholder="01" />';
    html += '<input class="year" maxlength="4" placeholder="2016"/>';
    html +="<br><br>";
    html +='<p><input class="smoker" type="checkbox"/>Please verify that you are a smoker.</p>';
    html +="<br>";
    html += '<button>Submit</button></div></div>';

Validation script:

 validate : function(){
 _this.errors = [];
 if (/^([0-9]|[12]\d|3[0-1])$/.test(_this.day) === false) {
 _this.errors.push('Day is invalid or empty');
 };
 if (/^(19|20)\d{2}$/.test(_this.year) === false) {
 _this.errors.push('Year is invalid or empty');
 };
 _this.clearErrors();
 _this.displayErrors();
 return _this.errors.length < 1;
 },

I played around a bit with the following code but something is missing:

if ("Not sure what to enter here to validate the checkbox.".test(_this.smoker) === false) {
_this.errors.push('You have not selected if you are a smoker');
};




Aucun commentaire:

Enregistrer un commentaire