如何仅在第一个字符中不允许-
(连字符)?
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Remove Special Characters from the Textbox - Coding cluster</title>
<script language="javascript" type="text/javascript">
function deleteSpecialChar(txtName) {
if (txtName.value != '' && txtName.value.match(/^[\w ]+$/) == null)
{
txtName.value = txtName.value.replace(/[\W]/g, '');
}
}
</script>
</head>
<body>
<form id="form1" >
<div>
<input id="txtName" type="text" onkeyup="javascript:deleteSpecialChar(this)" />
</div>
</form>
</body>
</html>
答案 0 :(得分:1)
应该是:
txtName.value = txtName.value.replace(/^[\W]/, '');
^ ^
| |
replace by first char |
|
global flag removed
要在此行上方的代码后面的文本中替换--
:
txtName.value = txtName.value.replace(/\-+/g, '-');
^
|
means 1 or more occurrences
已更新fiddle。