繁体   English   中英

表单验证问题,代码不起作用

[英]Form validation problem, code doesn't work

我真的不知道为什么这个验证不起作用。

这是我的HTML:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type='text/javascript' src="scripts.js"></script>
<title>Test Page!</title>
</head>

<body>
<form onsubmit ="return validateFormOnSubmit(this)" name="myForm" action = "testForm.html">
<input name="textField" type="text" value="" size="50" id= "textfield"  /> <br />
<input name="button" type="submit" value="Submit"/>
</form>
</body>
</html>

这是我的JavaScript文件:

function isURL(fld){
    var error = "";
    var regex = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    if(regex.test(fld.value)){
        error = "No URL's Allowed!";
    }
    else{
        error = "";
    }
    return error;
}

function validateFormOnSubmit(myForm) {
var reason = "";

  reason += isURL(myForm.textField);


  if (reason != "") {
    alert("there's something wrong: \n" + reason);
    return false;
  }

  return true;
}

谢谢你的帮助。

更新:真的需要采取行动吗? testpage.html只是一个空的html文件!

更新2:问题是我没有看到任何消息或警报。

工作正常,您的scripts.js文件未正确加载或浏览器中缓存了旧版本

看起来您正在传递表单,而不是您要验证的字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM