Click here to Skip to main content
Click here to Skip to main content

JavaScript Validation for textbox Special Character Selection

, 31 Mar 2014
Rate this:
Please Sign up or sign in to vote.
JavaScript validation for textbox special character selection

Introduction

To enter validation in textbox, we can use JavaScript code. In ASP.NET, if we want to include validation in the textbox defined in aspx page, first create a JavaScript page. To create a JavaScript page, select "Add New Item" from "Website" menu. Then select "Jscript File". Write the below code in your "Jscript.js" file.

Using the Code

function chkChars(type,SChars)
{

// type 
// 'A' ALPHABETS
// 'N' NUMERIC
// 'AN' APLPHA NUMERIC

// SChars :- All the Special Character that you don't want to check, pass them like -!=+


if(SChars.indexOf(String.fromCharCode(window.event.keyCode))>=0)
{
return true;
}
if (window.event.keyCode==32)  // Check For Space Key
{
return true;
}
if (window.event.keyCode==13)  // Check For Enter Key
{
return true;
}

switch(type)
    {                
        case 'A':    // Parameter 'A' used for Alphabets Only
            {        
                if ((window.event.keyCode<65 || window.event.keyCode>122) && 
                (window.event.keyCode<90 || window.event.keyCode>97) ) 
                {
                window.event.returnValue=false;
                alert("Please Enter Only Alphabets");
                }
                break;
            }        // Case A End

        case 'N':    // Parameter 'N' used for Numbers Only
            {
                if ((window.event.keyCode<48 || window.event.keyCode>57)) 
                {
                window.event.returnValue=false;
                alert("Please Enter Only Numeric Values");
                }
                break;
            }
        case 'AN':
            {
                if ((window.event.keyCode<48) ||(window.event.keyCode>57 && 
                window.event.keyCode<65) || window.event.keyCode>122 || 
                (window.event.keyCode>90 && window.event.keyCode<97)) 
                {
                window.event.returnValue=false;
                alert("Please Enter Only Alpha Numeric Values");
                }
                break;
            }    
    }            
} 

After writing this code in "Jscript.js" file, in "Default.aspx", include:

<script language="JavaScript" type="text/JavaScript" src="JavaScript/CFunc.js">

in the <head></head> tag of your Default.aspx page. The work is almost done. Now just use "chkchars('AN','')" for alphanumeric validation."chkchars('A','')" for Alphabet validation, "chkchars('N','')" for Numeric validation and to exclude some characters during validation, just add it in chakchars function as"chkchars('AN','()/-')" for validating (,),- and / in that textbox.
For example:

<asp:TextBox ID="textbox1" runat="server" onKeyPress="chkChars('AN','()-/')" ></asp:TextBox>

This example will allow only Alphanumeric Characters or (,),-,/.

Hope this will help you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Silvercorer

India India
No Biography provided

Comments and Discussions

 
QuestionNeeds more work PinmemberWombaticus1-Apr-14 12:09 
AnswerRe: Needs more work PinmemberSilvercorer1-Apr-14 19:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 1 Apr 2014
Article Copyright 2014 by Silvercorer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid