公司网站上有让用户匿名留言的功能,但是有三个必须的条件,公司或个人名称,联系人,联系电话。对这三个的要求既不想让输入空串就通过,还不想让随便输点东西就通过,特别是
电话要求不想太严格,又想让随便就蒙混过去。一下是测试程序。用户名和电话不能为空,电话号码至少含有7位以上的数字。
<html>
<head>
<title>测试</title>
</head>
<body>
<form action="#">
用户名:<input name="username" id="un">
电话:<input name="tel" id="tel">
<input type="submit" onclick="check()" value="提交">
</form>
<script>
function check(){
var _username = document.getElementById("un").value;
var _tel = document.getElementById("tel").value;
<!-- 去掉字符串两侧的空格 -->
_username = _username.replace(/(^\s*)|(\s*$)/g, "");
_tel = _tel.replace(/(^\s*)|(\s*$)/g, "");
if(_username !="" && _tel !="") {
alert(_username);
alert(_tel);
}
<!-- 电话号码中必须含有7位以上的数字 -->
var tel = _tel.match("[0-9]{7}");
if(tel != null) {
alert(_tel);
}
return false;
}
</script>
</body>
</html>
电话要求不想太严格,又想让随便就蒙混过去。一下是测试程序。用户名和电话不能为空,电话号码至少含有7位以上的数字。
<html>
<head>
<title>测试</title>
</head>
<body>
<form action="#">
用户名:<input name="username" id="un">
电话:<input name="tel" id="tel">
<input type="submit" onclick="check()" value="提交">
</form>
<script>
function check(){
var _username = document.getElementById("un").value;
var _tel = document.getElementById("tel").value;
<!-- 去掉字符串两侧的空格 -->
_username = _username.replace(/(^\s*)|(\s*$)/g, "");
_tel = _tel.replace(/(^\s*)|(\s*$)/g, "");
if(_username !="" && _tel !="") {
alert(_username);
alert(_tel);
}
<!-- 电话号码中必须含有7位以上的数字 -->
var tel = _tel.match("[0-9]{7}");
if(tel != null) {
alert(_tel);
}
return false;
}
</script>
</body>
</html>
本文介绍了一种前端验证技术,用于确保用户提交的数据符合基本要求。具体包括去除输入两端的空白字符,并检查用户名和电话号码是否为空,同时确保电话号码包含至少7位数字。
853

被折叠的 条评论
为什么被折叠?



