Click here to Skip to main content
12,508,630 members (54,456 online)
Click here to Skip to main content
Add your own
alternative version

Stats

47.2K views
291 downloads
7 bookmarked
Posted

TextBox only write String Character

, 4 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
If user try To write Non String character in TextBox that character won't be written

Introduction

In this article I will show how to make the script control TextBox have the following value: " Enter String Only " When the user clicks on the TextBox it becomes blank, and when the user deletes the written value from " Enter String Only " it re-requests an entry, and if the user tries to write a non-string character in the TextBox that character will not be written.

Using the Code

inputtext_onclick()

When the user deletes words from the entry " Enter String Only " fulltext_befor_new_char is set to "" or to the TextBox value that function fires when the user clicks on the TextBox.

function inputtext_onclick() 
{ 
// if the TextBox value = "Enter String Only" that mean
// the use not written any words yat so set the textbox
// value to "" and fulltext_befor_new_char Variable to "" 
if ((TextBox_Element.value == "Enter String Only")) 
{ 
TextBox_Element.value=""; 
fulltext_befor_new_char = ""; 
} 
Else 
// else mean mean the user written words befor as save it in
//fulltext_befor_new_char Variable 
fulltext_befor_new_char = TextBox_Element.value; 
}

inputtext_onblur()

This function is responsible for rewriting " Enter String Only " words if the user doesn't write any words that the function fires when the TextBox loses focus.

function inputtext_onblur() 
{ 
// if the textbox value = "" then the use laft the text box empty so
//set textbox value to "Enter String Only" agane 
if ((TextBox_Element.value == "")) 
TextBox_Element.value="Enter String Only"; 
}

inputtex_onkeydown(e)

function inputtex_onkeydown(e)
{ 
// cheak if the user use firefox
if (IsFireFox())
{
    // if firefox use e.which
    var key_code = e.which ;
}
else 
{
    // if not firefox use event.keyCode
    var key_code = event.keyCode ;
}
// cheak if the user press backspace key to delete writen character
// if not backspace then cheak 
if (key_code != 8)
{
    var ch = String.fromCharCode(key_code);
    var filter = /[a-zA-Z]/ ;
    if(!filter.test(ch))
    {
        //Cancel the Input
        if (IsFireFox())
        e.preventDefault(); 
        else
        event.returnValue = false ;
    }
}
} 
function inputtext_onkeypress() 
{ 
var fulltext = TextBox_Element.value; 
// if character legal then save textbox value in
//fulltext_befor_new_char Variable texbox value to 
if (Filter(fulltext)) 
{ 
fulltext_befor_new_char = TextBox_Element.value; 
} 
Else 
// if character illegal then set the texbox value to
//fulltext_befor_new_char ( old textbox value ) 
{ 
TextBox_Element.value = fulltext_befor_new_char; 
} 
}

function inputtex_onchange()

The function responsible for checking if the value is legal or illegal if the user pastes or drops the value:

function inputtex_onchange()
{
var inputtex_vlaue = TextBox_Element.value;
var filter = /^[a-zA-Z]+$/ ;
if(!filter.test(inputtex_vlaue))
    {
        TextBox_Element.value = "Enter String Only";
    }
}
function
add_Events_To_TextBox(TextBox_Name_Client)

The function responsible on adding (onkeydownonclickonbluronchange) events to the textbox in RunTime that the function fires after the document on page Load.

function add_Events_To_TextBox(TextBox_Name_Client) 
{ 

TextBox_Element = document.getElementById(TextBox_Name_Client);
TextBox_Element.onkeydown=function(event)
                    {
                        inputtex_onkeydown(event);
                    };
TextBox_Element.onclick=inputtext_onclick ;
TextBox_Element.onblur=inputtext_onblur;
TextBox_Element.onchange = inputtex_onchange;
}

In the ASPX file write at end of <form></form> element:

<script language="javascript" type="text/javascript" src="Unwrite_aspx.js" > 
</script> 
<script language="javascript" type="text/javascript" > 
add_Events_To_TextBox("<%=Unwrite_TestBox.ClientID%>"); 
</script>

Happy coding …

License

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

Share

About the Author

mohamed antar
Web Developer
Egypt Egypt
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionAnother Solution Pin
Mostafa Metwally2-Mar-13 9:31
memberMostafa Metwally2-Mar-13 9:31 
Generalstring?! Pin
Adam Tibi6-Aug-07 5:43
memberAdam Tibi6-Aug-07 5:43 
GeneralRe: string?! Pin
mohamed antar6-Aug-07 6:25
membermohamed antar6-Aug-07 6:25 
GeneralMore simple solution Pin
Alex.Shnaider29-Jul-07 3:53
memberAlex.Shnaider29-Jul-07 3:53 
GeneralRe: More simple solution Pin
mohamed antar29-Jul-07 23:07
membermohamed antar29-Jul-07 23:07 
GeneralRe: More simple solution Pin
Louisa Chen30-Jul-07 16:22
memberLouisa Chen30-Jul-07 16:22 
GeneralRe: More simple solution Pin
Alex.Shnaider30-Jul-07 19:19
memberAlex.Shnaider30-Jul-07 19:19 
GeneralRe: More simple solution Pin
mohamed antar30-Jul-07 20:06
membermohamed antar30-Jul-07 20:06 
GeneralLittle Formatting is required Pin
Vasudevan Deepak Kumar28-Jul-07 21:50
memberVasudevan Deepak Kumar28-Jul-07 21:50 
GeneralRe: Little Formatting is required Pin
mohamed antar29-Jul-07 23:07
membermohamed antar29-Jul-07 23:07 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 4 Aug 2007
Article Copyright 2007 by mohamed antar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid