Tuesday 6 February 2018

Generate Promo Code / Random String

function generateRandomPromo()
{
    length = 6;
    chars = '#aA';
    var mask = '';
    if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
    if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    if (chars.indexOf('#') > -1) mask += '0123456789';
    if (chars.indexOf('!') > -1) mask += '~`!@#$%^&amp;*()_+-={}[]:";\'<>?,./|\\';
    var result = '';
    for (var i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))];
    $('#EventPromocode_promocode').val(result);
}

Validation Form

HTML

<form onsubmit="return validateQuestionForm();">

JAVASCRIPT

function validateQuestionForm(){
   
    var flag=true;
    var err=false;
    var err_print = "Error :<br>";
   
    // check titile
    if($('#title').val()=="") {
        err = true;
        err_print += "- Please insert question title.<br>";
        $('#title').css({
            "border": "1px solid red",
            "background": "#FFCECE"
        });
    } else {
        $('#title').css({
            "border": "",
            "background": ""
        });
    }
   
    // check option and checkbox which id = correct_radio_
    if($('input:radio[id^="correct_radio_"]').is(":checked") || $('input:checkbox[id^="correct_radio_"]').is(":checked")) {
    } else {
            err = true;
            err_print += "- Please select correct option.<br>";
    }
   
    // check option text box which class = correct_txt
    $('input[class="correct_txt"]').each(function() {
        if ($.trim($(this).val()) == '') {
            err = true;
            if(flag==true) {
                err_print += "- Please insert question answer.<br>";
                flag=false;
            }
            $(this).css({
                "border": "1px solid red",
                "background": "#FFCECE"
            });
           
        } else {
            $(this).css({
                "border": "",
                "background": ""
            });
        }
    });
   
    // error print
    if(err==true) {
        $('#error_notification').removeClass('displaynone').html(err_print);
        return false;
    } else {
        return true;
    }
}
 
// type change radio to checkbox || checkbox to radio
function changeAnswerType(current_type,change_type,answer_type){
   
    $('#typechange').find('input:'+current_type).attr('type',change_type,'checked','');
    if(answer_type=='single') {
        $('#addmore').attr('onclick','htmlChange("single","second")');
    }else {
        $('#addmore').attr('onclick','htmlChange("multiple","second")');
    }
 
}

Yii Interview Question