Saturday 17 May 2014

Text box has to allow only characters in Asp.Net


In Text box Design Coding:

<asp:TextBox ID="txtAffwith" runat="server" BackColor="White" onkeypress="return isNameKey(event)"
                    Font-Names="Verdana" Font-Size="9pt" Height="18px" Width="190px"
                                MaxLength="50"></asp:TextBox>


Text box has to allow only characters:

function isNameKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode <= 93 && charCode >= 65) || (charCode <= 122 && charCode >= 97){
            return true;
        }
        else {
            return false;
        }
    }


Text box has to allow only characters with Space:

function isNameKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode <= 93 && charCode >= 65) || (charCode <= 122 && charCode >= 97) || (charCode == 32) ){
            return true;
        }
        else {
            return false;
        }
    }

Text box has to allow only numbers:


function isNumberKey1(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode < 48 || charCode > 57) {
            return false;
        }
        else {
            return true;
        }
    }



Text box has to allow only numbers with dots:


function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode != 46 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        else {
            return true;
        }
    }


jQuery - Allow Alphanumeric (Alphabets & Numbers)

 Characters in Textbox using JavaScript


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Allow only alphanumeric characters in textbox</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
e.preventDefault();
}
}
});
});
</script>
</head>
<body>
<div>
<b>Enter Text:</b><input type="text" id="txtNumeric" />
</div>
</body>
</html>






No comments:

Post a Comment