Click here to Skip to main content
15,914,419 members
Articles / Web Development / ASP.NET
Tip/Trick

JavaScript Validation for textbox Special Character Selection

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
31 Mar 2014CPOL 26.6K   5   2
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

JavaScript
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:

JavaScript
<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.NET
<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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNeeds more work Pin
Wombaticus1-Apr-14 12:09
Wombaticus1-Apr-14 12:09 
AnswerRe: Needs more work Pin
Silvercorer1-Apr-14 19:14
Silvercorer1-Apr-14 19:14 

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

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