validation

Discussion in 'jQuery' started by Geetha Manoharan, Mar 15, 2012.

  1. #1
    How can i validate the gender field (2 radio buttons) and the education field (4 check boxes) and store the selected values in the database...Any one please help me to do this...
     
    Geetha Manoharan, Mar 15, 2012 IP
  2. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #2
    PHP can do that. Here, have a tutorial that will teach you the basics of PHP (will also help you with your problem)

    http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/
    Code (markup):
     
    GMF, Mar 16, 2012 IP
  3. Geetha Manoharan

    Geetha Manoharan Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi GMF i have validated that using php but now my requirement is validateing a form using jquery..

    I can validate the text boxes, dropdown list etc in the form. But i dont have any clear idea for validating the radio buttons and check boxes..I have attached the script that i have made to validate my form..All other fields gets validated perfectly except the gender and education...Iam strucking with this Please help me to make it done..

    My script is,

    <script type="text/javascript" >
    $(function() {
    $('.error').hide();
    $(".submit").click(function(){
    $('.error').hide();
    var name = $("input#txtName").val();
    var password = $("input#txtPass").val();
    var age = $("input#txtAge").val();
    var day = $("select#day").val();
    var month = $("select#month").val();
    var year = $("select#year").val();
    //var gender1 = $("input#rdGendr1").val();
    //var gender2 = $("input#rdGendr2").val();
    //var edu = $("input#edu").val();
    boxes = document.form.Edu.length
    var txt = ""
    for (i = 0; i < boxes; i++) {
    if (document.form.Edu.checked) {
    txt = txt + document.form.Edu.value + " "
    }
    }
    var yourself = $("textarea#txtYourself").val();
    var interest = $("select#txtInterest").val();

    // REGULAR EXPRESSIONS
    var ck_name = /^[A-Za-z0-9'. ]{1,20}$/;
    var ck_password = /^[A-Za-z0-9!@$*_.]{6,20}$/;
    var ck_age=/^[0-9]{2}$/;

    if(name == "")
    {
    $("label#name_error").show();
    $("input#txtName").focus();
    return false;
    }
    else if(!ck_name.test(document.getElementById('txtName').value))
    {
    $("label#name_error1").show();
    $("input#txtName").focus();
    return false;
    }
    else if(password == "")
    {
    $("label#pass_error").show();
    $("input#txtPass").focus();
    return false;
    }
    else if(!ck_password.test(document.getElementById('txtPass').value))
    {
    $("label#pass_error1").show();
    $("input#txtPass").focus();
    return false;
    }
    else if(age == "")
    {
    $("label#age_error").show();
    $("input#txtAge").focus();
    return false;
    }
    else if(!ck_age.test(document.getElementById('txtAge').value))
    {
    $("label#age_error1").show();
    $("input#txtAge").focus();
    return false;
    }
    else if(day == "day0")
    {
    $("label#day_error").show();
    $("select#day").focus();
    return false;
    }
    else if(month == "month0")
    {
    $("label#month_error").show();
    $("select#month").focus();
    return false;
    }
    else if(year == "year0")
    {
    $("label#year_error").show();
    $("select#year").focus();
    return false;
    }
    else if(gender1 == "" && gender2 == "")
    //else if((form.gender[0].checked=="")&&(form.gender[1].checked==""))
    {
    $("label#gender_error").show();
    $("input#rdGendr1").focus();
    return false;
    }

    else if(txt == "")
    //else if((form.Edu[0].checked==false)&&(form.Edu[1].checked==false)&&(form.Edu[2].checked==false)&&(form.Edu[3].checked==false))
    {
    $("label#edu_error").show();
    $("input#chkEdu1").focus();
    return false;
    }
    else if(yourself == "")
    {
    $("label#your_error").show();
    $("textarea#txtYourself").focus();
    return false;
    }
    else if(interest == "select")
    {
    $("label#interest_error").show();
    $("select#txtInterest").focus();
    return false;
    }

    var txtName = $('#txtName').val();
    var txtPass = $('#txtPass').val();
    var txtAge = $('#txtAge').val();
    var day = $('#day').val();
    var month = $('#month').val();
    var year = $('#year').val();
    //var gender = $('div#gender').val();
    var gender = document.getElementByName('gender').val();
    //var edu = $('div#edu').val();
    var txtYourself = $('#txtYourself').val();
    var txtInterest = $('#txtInterest').val();
    var upload = $('#upload').val();
    alert(txtName );
    dataString = "txtName="+txtName +"&txtPass="+txtPass+"&txtAge="+txtAge+"&day="+day+"&month="+month+"&year="+year+"&gender="+gender+"&edu="+txt+"&txtYourself="+txtYourself+"&txtInterest="+txtInterest+"&upload="+upload+"&formsubmit=submit" ;
    alert(dataString)
    $.ajax({
    type: "POST",
    url: "storeDB.php",
    data: dataString,
    success: function(){
    $('.success').fadeIn(200).show();
    $('.err').fadeOut(200).hide();

    }

    });
    });
    });
    </script>
     
    Geetha Manoharan, Mar 20, 2012 IP
  4. kuldipinfotech

    kuldipinfotech Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    From the code, I can find out you are using very basic level code for input field as well. Better try to user validation.js from JQuery. Easy to use and will solve your purpose I believe.If we talk about your code,
    [LEFT][SIZE=2][COLOR=#00008B][FONT=Consolas]  [FONT=century gothic]
    [/FONT]if[/FONT][/COLOR][COLOR=#000000](!$[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]'input[type="checkbox"]'[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#00008B]is[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]':checked'[/COLOR][COLOR=#000000])){ 
      // show your error message 
     }[/COLOR][/SIZE][/LEFT]
    
    Code (markup):
     
    kuldipinfotech, Mar 27, 2012 IP