仅允许TextBox写入字符串字符






2.53/5 (7投票s)
如果用户尝试在TextBox中写入非字符串字符,该字符将不会被写入。
引言
在本文中,我将展示如何使脚本控件TextBox
具有以下值:“仅输入字符串
”。当用户单击TextBox
时,它会变成空白,当用户删除“仅输入字符串
”中已写入的值时,它会重新请求输入,如果用户尝试在TextBox
中写入非字符串字符,该字符将不会被写入。
Using the Code
inputtext_onclick()
当用户从“仅输入字符串
”条目中删除单词时,fulltext_befor_new_char
被设置为""或TextBox
的值,该函数在用户单击TextBox
时触发。
function inputtext_onclick()
{
// if the TextBox value = "Enter String Only", that means
// the user has not written any words yet, 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 means the user has written words before as save it in
// fulltext_befor_new_char Variable
fulltext_befor_new_char = TextBox_Element.value;
}
inputtext_onblur()
此函数负责在用户未写入任何单词时重写“仅输入字符串
”单词,该函数在TextBox
失去焦点时触发。
function inputtext_onblur()
{
// if the textbox value = "" then the use left the text box empty so
// set textbox value to "Enter String Only" again
if ((TextBox_Element.value == ""))
TextBox_Element.value="Enter String Only";
}
inputtex_onkeydown(e)
function inputtex_onkeydown(e)
{
// check if the user uses Firefox
if (IsFireFox())
{
// if Firefox uses e.which
var key_code = e.which ;
}
else
{
// if not Firefox, use event.keyCode
var key_code = event.keyCode ;
}
// check if the user presses backspace key to delete written character
// if not backspace, then check
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()
负责检查值是否合法或非法,如果用户粘贴或拖放值,则该函数负责检查。
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)
负责在RunTime
中将(onkeydown
– onclick
– onblur
– onchange
)事件添加到textbox
的函数,该函数在页面加载后触发。
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;
}
在ASPX文件中,在<form></form>
元素末尾写入
<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>
编码愉快…
历史
- 2007年8月4日:初始版本