Tuesday 6 February 2018

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")');
    }
 
}

No comments:

Yii Interview Question