如何编写动态javascript函数,只允许文本框接受字母值 [英] How can I write a dynamic javascript function that only allow text boxes to accept alphabet values
问题描述
大家好。
我正在开发一个基于网络的系统,我希望我的文本框只接受字母值。我怎么能用javascript或jquery实现这一点kinldly告诉我一些代码请...
i尝试了如下函数,但这个函数只能在按钮和一个文本框我想要动态代码,无论何时我都会打电话给它。
我尝试过:
function onlyAlphabets(){
var regex = / ^ [a-zA-Z] * $ /;
if( regex.test(document.getElementById(txtdetail)。value)){
返回true;
} else {
alert(只有字母表。);
返回false;
}
}
/;
if(regex.test(document.getElementById(txtdetail)。value)){
返回true;
} else {
alert(仅限字母表。);
返回false;
}
}
HI ,
尝试创建一个通用函数,如下所示:
函数onlyAlphabets(strTextboxIdentity){
var regex = / ^ [a-zA-Z] *
/;
if(regex.test(document.getElementById(strTextboxIdentity).value)) {
返回true;
}否则{
返回false;
}
}
使用上述功能可以检查文本框的字母字符:
var结果= onlyAlphabets(txtdetail);
if(Result == false)
{
alert(仅限字母表。);
返回;
}
其他
{
//您的条件是必需的这里。
}
Hi Everyone.
I am developing a web based system and i want that my text boxes accept alphabet values only. how can i implement this with javascript or jquery kinldly show me some code please...
i have tried a function as follows but this function does work only in button and in one text box i want dynamic code that work everywhere whenever i will call to it.
What I have tried:
function onlyAlphabets() {
var regex = /^[a-zA-Z]*$/;
if (regex.test(document.getElementById("txtdetail").value)) {
return true;
} else {
alert("Alphabets Only Please.");
return false;
}
}
/;
if (regex.test(document.getElementById("txtdetail").value)) {
return true;
} else {
alert("Alphabets Only Please.");
return false;
}
}
HI ,
Try to create a generalize function , Like the one :
function onlyAlphabets(strTextboxIdentity) {
var regex = /^[a-zA-Z]*
/;
if (regex.test(document.getElementById( strTextboxIdentity ).value)) {
return true;
} else {
return false;
}
}
Use the above function wherever you want to check alphabet characters for textbox :
var Result = onlyAlphabets(txtdetail);
if(Result == false)
{
alert("Alphabets Only Please.");
return ;
}
else
{
// Your condition which is required here.
}
这篇关于如何编写动态javascript函数,只允许文本框接受字母值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!