Textbox problem with regex
I have used a regex function on a textbox, but it's not working.
<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;
}
I have restricted it for alphabets, hyphen, dots, and single quotes.开发者_开发问答
Your problem is that you are using a function named title
that happens to have the same name as your id
.
To fix, just change your function name. For example, the following works:
<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;
}
You can write javascript code like:
<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;
}
精彩评论