3

I am using the following regular expression to check name format.

/^([A-Za-z0-9\s_@.-])*$/

Name should contain only alphanumeric characters,_,@,.,-and space. But this regular expression is not restricting '\'.

4

4 回答 4

2

尝试这个

[A-Za-z_-@。0-9]{1,250}

于 2012-06-12T13:52:50.673 回答
0

我修改了您的表达式,因为/^([A-Za-z0-9 _@.-])*$/; 给出了 \s 空间

尝试这个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript">

function chkit()
{
    var password=document.getElementById('password').value;
   var namep=/^([A-Za-z0-9 _@.-])*$/;

    var name=document.getElementById('name').value;
    if(name!="")
    {
        if(!namep.test(name))
            {
                alert('name not in format');
            }
            else
            {
                alert('name In format');
            }
    }
}

</script>
</head>

<body>
  <p>
    <label for="name">name</label>
    <input name="name" type="text" id="name" onblur="chkit();"  />
  </p>
</body>
</html>
于 2012-06-12T06:44:55.143 回答
0

您的代码正在运行:

Test 1:
"\\".search(/^([A-Za-z0-9\s_@.-])*$/);

//-1

Test 2:
"Apple\\Microsoft".search(/^([A-Za-z0-9\s_@.-])*$/);

//-1
于 2012-06-12T05:53:46.210 回答
0

你可以试试这个

if (/^([\w\s@.-]+)$/i.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}
于 2012-06-12T06:03:47.733 回答