Saturday 7 September 2013

Javascript Validation For Enter only Alphabet Character in TextBox in Javascript




Description:-

            In this Example we Explain that How to put validation on the TextBox that will only allow alphbet Character and other Special Character or Number are not allowed by using Javascript.

Here we use the Event Object that are used to Fetch the ASCII code of the Character that is entered by user in the TextBox. If Ascii code is Correspond to Alphabet then It will allow otherwise the other character are not even written in Textbox means Disable the Other Key.

What is Validation:-

                        
Validation Means to check the Data at Client side that the insert data by the user is Valid or not. if valid then and then the Request is transfer to the server oterwise it will Display a proper message like Only Digit is Allowed in this Checkbox.

so by using this Validation Functionality we will reduce the Traffic or load in the Server. so validation is must Required in any form or application.
 

What is Event Object:-

Event Object are Generally used within Fuction and are used to Generate the Event.Some Important Property of the Event Object are as Follows:

Altkey :- Returns whether or not the "ALT" key was pressed when an event was triggered or Executed

Button :- Returns whether or not the "ALT" key was pressed when an event was triggered

clientX :- Returns the horizontal coordinate of the mouse pointer, relative to the current window, when an event was triggered.

clientY :- Returns the vertical coordinate of the mouse pointer, relative to the current window, when an event was triggered.

Ctrlkey :- Returns whether or not the "CTRL" key was pressed when an event was triggered

Metakey : - Returns whether or not the "meta" key was pressed when an event was triggered

screenX :- Returns the horizontal coordinate of the mouse pointer, relative to the screen, when an event was triggered.

screenY :- Returns the vertical coordinate of the mouse pointer, relative to the screen, when an event was triggered

import contact From Gmail in our website in Asp.Net How to use Gmail contacts in our website


Implement Remember Me functionality using CheckBox ASP.Net 

set WaterMark Text in PDF using itextsharp in C#

How to set Default Button in MVC Web Form Application 

How to Bind XML File data to Treeview in asp.net

JQuery datepicker calender with Dropdown month and year in asp.net.





Code:-



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="kodemo.aspx.cs" Inherits="kodemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.7.1.js">
      
    </script>
    <script src="Scripts/jquery-1.7.1.min.js"></script>
    <script src="Scripts/knockout-2.1.0.js"></script>
    <script type="text/javascript">
        function Alphabets(nkey) {
            var keyval
            if (navigator.appName == "Microsoft Internet Explorer") {
                keyval = window.event.keyCode;
            }
            else if (navigator.appName == 'Netscape') {
                nkeycode = nkey.which;
                keyval = nkeycode;
            }
            //For A-Z
            if (keyval >= 65 && keyval <= 90) {
                return true;
            }
                //For a-z
            else if (keyval >= 97 && keyval <= 122) {
                return true;
            }
                //For Backspace
            else if (keyval == 8) {
                return true;
            }
                //For General
            else if (keyval == 0) {
                return true;
            }
                //For Space
            else if (keyval == 32) {
                return true;
            }
            else {
                return false;
            }
        }// End of the function


    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <p>Enter only Ahphabet Character</p>
            <asp:TextBox ID="txtUsername"
                onkeypress="return Alphabets(event);"
                runat="server" />
        </div>
    </form>
</body>
</html>


16 comments:

  1. kirit kapupara sir give the another server for send the sms

    ReplyDelete
  2. I wnt to use only expression dnt use keycode
    plz czn u help me out

    ReplyDelete
  3. script type="text/javascript"
    var r={
    'special':/[\W]/g,
    'quotes':/['\''&'\"']/g,
    'notnumbers':/[^\d]/g,
    'notletters':/[A-Za-z]/g,
    'numbercomma':/[^\d,]/g,
    }

    function valid(o,w){
    o.value = o.value.replace(r[w],'');
    }
    /script

    HTML

    input type="text" name="login" onkeyup="valid(this,'numbercomma')" /

    ReplyDelete
  4. This is really nice article have a look of this also

    http://www.aspdotnet-pools.com/2014/06/allow-only-alphabets-in-textbox-using.html

    ReplyDelete
  5. Hai its really nice article and its working fine. But if i open the site from proxy it's allowing to enter all the values. Is there any solution for that ? Im using PHP application.

    ReplyDelete
  6. Xcellent Script for All Browsers....Thank u dear

    ReplyDelete
  7. refer this link for perfect answer
    https://www.youtube.com/watch?v=KS1dV34xtzk

    ReplyDelete