正则表达式的文本框问题

Textbox problem with regex

本文关键字:问题 文本 正则表达式      更新时间:2023-09-26

我在一个文本框上使用了一个正则表达式函数,但是它不起作用。

<input name="title" class="txtbxinput" id="title"
onblur="showsongstatus(this.value);title(this);" 
onkeyup="title(this);" size="58" >
function title(f) {
   f.value = f.value.replace(/[^a-zA-Z's.'-]/gixsm,'');
   f.value = f.value.replace(/'s's+/g, ''s'); //remove more than 2 white spaces.
   f.value = f.value.replace(/'{2,}/g, '''');
   f.value = f.value.replace(/--/g, '-');
   f.value = f.value.replace(/'.'./g, ''.');
return f;
}

我限制了字母、连字符、点和单引号。

您的问题是您正在使用一个名为title的函数,恰好与您的id具有相同的名称。

要修复,只需更改函数名称。例如:

<input name="title" class="txtbxinput" id="title"
onblur="showsongstatus(this.value); doTitle(this);" 
onkeyup="doTitle(this);" size="58" >
function doTitle(f) {
   f.value = f.value.replace(/[^a-zA-Z's.'-]/gixsm,'');
   f.value = f.value.replace(/'s's+/g, ''s'); //remove more than 2 white spaces.
   f.value = f.value.replace(/'{2,}/g, '''');
   f.value = f.value.replace(/--/g, '-');
   f.value = f.value.replace(/'.'./g, ''.');
return f;
}

你可以写这样的javascript代码:

<input type="text" id="txt" onkeypress="return check(event);" />
function check(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode            
            if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || charCode == 39 || charCode == 46 || charCode == 45)
                return false;
            else
                return true;
        }