Saturday, 25 July 2015

Jquery validations(text box allow only numerics)

(1)Allow only numeric in textbox control
=======================================================

<asp:TextBox ID="txtMobileNo" runat="server" required="true"
                  x-moz-errormessage="Mobile No is required along with formate." MaxLength="12" pattern="[0-9]{12,12}"
                  onkeypress="return validateAllowOnlyNumerics(event);"></asp:TextBox>

function validateAllowOnlyNumerics(nkey)
{
    var keyval
    if (navigator.appName == "Microsoft Internet Explorer") {
        keyval = window.event.keyCode;
    }
    else if (navigator.appName == 'Netscape') {
        nkeycode = nkey.which;
        keyval = nkeycode;
    }

    //For 0-9
    if (keyval >= 48 && keyval <= 57) {
        return true;
    }
        //For General
    else if (keyval == 0) {
        return true;
    }
        //For Backspace
    else if (keyval == 8) {
        return true;
    }
    else {
        return false;
    }
}


$(function () {
    $("[id*=btnGet]").click(function () {
        var checked_radio = $("[id*=rblFruits] input:checked");
        var value = checked_radio.val();
        var text = checked_radio.closest("td").find("label").html();
        alert("Text: " + text + " Value: " + value);
       return false;
    });
});

$(function () {
    $("[id*=btnSet_Text]").click(function () {
        var text = "Mango";
        var radio = $("[id*=rblFruits] label:contains('" + text + "')").closest("td").find("input");
        radio.attr("checked""checked");
        return false;
    });
});


$(function () {
    $("[id*=btnSet_Value]").click(function () {
        var value = "2";
        var radio = $("[id*=rblFruits] input[value=" + value + "]");
        radio.attr("checked""checked");
        return false;
    });
});


//Get value of selected items
$("#demo").click(function () {     
    var selectedValues = [];
    $("[id*=CheckBoxList1] input:checked").each(function () {          
        selectedValues.push($(this).val());
    });
    if (selectedValues.length>0) {
        alert("Selected Value(s): " + selectedValues);
    } else {
        alert("No item has been selected.");
    }
});

//Get text of selected items
 $("#demo").click(function () {      
     $("[id*=CheckBoxList1] input:checked").each(function () {
         alert($(this).next().html());
     });
 });

$("[id*=CheckBoxList1] input:checkbox").prop('checked',true); //To check all
$("[id*=CheckBoxList1] input:checkbox").prop('checked',false);// To uncheck all

//Check Items by value
   var selValue = [1, 2, 4];
   var $ctrls = $("[id*=CheckBoxList1]");
   for (var i = 0; i < selValue.length; i++) {
       $ctrls.find('input:checkbox[value=' + selValue[i] + ']').prop('checked', true);
   }


//Check Items by Text
    var selText = ['Item-1','Item-3'];
    var $ctrls = $("[id*=CheckBoxList1]");
    for (var i = 0; i < selText.length; i++) {
        $ctrls.find('label:contains("' + selText[i] + '")').prev().prop('checked', true);
    }


$("[id*=CheckBoxList1] input:checkbox").change(function () {
          var maxSelection = 3;
          if ($("[id*=CheckBoxList1] input:checkbox:checked").length > maxSelection) {
              $(this).prop("checked", false);
              alert("Please select a maximum of " + maxSelection + " items.");
          }
      })
$("[id*=btnGet]").click(function () {

            var ddlFruits = $("[id*=ddlFruits]");
            var selectedText = ddlFruits.find("option:selected").text();
            var selectedValue = ddlFruits.val();
            alert("Selected Text: " + selectedText + " Value: " + selectedValue);
            return false;

        });



No comments:

Post a Comment