HTML 代码:
<html>
<head>
<title>Registration</title>
<script src="val_registration.js" type="text/javascript"></script>
</head>
<body>
<form action="" method="post" id="myform">
<table>
<tr>
<td><label for="Last_name">Last name:<span id="imp">*</span></label></td><td><input type="text" id="Last_name" tabindex="5"/>
<br/><span class="eg"> eg:Le You</span></td></td>
</tr>
<tr>
<td><label for="E_mail">E-mail:<span id="imp">*</span></label></td>
<td><input type="text" id="E_mail" tabindex="10"/>
<br/><span class="eg"> eg:abc123@hotmail.com</span></td></td>
</tr>
<tr>
<td colspan="4"><input type="submit" value="Confirm" id="confirm2" tabindex="11" onclick="val_registration ()"/>
<input type="reset" value="Cancel" id="cancel2" tabindex="12"/></td>
</tr>
</table>
</form>
</body>
</html>
JavaScript 代码:
function val_registration () {
var err = "";
var val_Last_name = document.getElementById("Last_name").value;
var string_Last_name = /^[a-zA-Z@'-_().,]{1,}$/;
if (val_Last_name == null || val_Last_name == "" || !string_Last_name.test(val_Last_name))
{
err += "\u2022Lastname cannot be blank/Lastname can contain\n alphabets or special symbols(@ ' - _ ( ).,) only.\n";
}
var val_E_mail = document.getElementById("E_mail").value;
var atpos = val_E_mail.indexOf("@");
var dotpos = val_E_mail.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2> = val_E_mail.length)
{
err += "\u2022E-mail cannot be blank/E-mail format must follow\n the example provided.\n";
}
alert(err);}
根据以下代码,我想问3个问题:
首先,为什么没有弹出警报?我的电子邮件验证有什么问题吗?
第二,为什么我有这个正则表达式(/^[a-zA-Z@'-_()., ]{1,}$/)?我只想让用户输入字母数据和上面正则表达式中给出的特殊符号。此外,我还发现数字和字母数据的组合也可以输入到此字段。为什么会这样?
第三,输入框的边框可以去掉吗?如果可以做到的话,那么该怎么做呢?
最佳答案
why didn't the alert pop up??Is there anything wrong with my e-mail validation??
您有> =
,当它应该是>=
时。并使用===
用于与 null
进行比较.
why erroneous data such as <, >, /, * and etc can be entered
因为你的正则表达式的一部分是 '-_
。 -
在 x-y
形式的字符类中表示允许 x
范围内的所有字符至y
,和<
落在 '
范围内和_
.
解决方案?使用
`/^[a-zA-Z@'\-_()\.,]{1,}$/`.
^-- escaped it
<小时/>
can I remove the border of input field? If this can be done, then how to do it?
用途:
input[type='text']{ /* only textboxes */
border:none; /*No border nor outline*/
outline:none;
-webkit-user-select:none; /*Disable user-select - the blue border when user clicks
TODO:add vendor prefixes for user-select*/
}
FULLY FIXED CODE
关于html 中的 JavaScript 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22706040/