You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在已实现非空校验的邮件列表表单中整合禁止输入超链接的校验逻辑

Got it, let's get this sorted for you! The fix is straightforward—we just need to plug that URL detection logic into your existing InputIsValid() function, right after the name field's non-empty check. Here's how to update your code:

Step 1: Update the Validation Function

We’ll add the hyperlink check immediately after verifying the name isn’t empty. This way, we first make sure the user entered a name, then check if that input contains a link.

Here’s the modified InputIsValid() function:

<SCRIPT LANGUAGE=JAVASCRIPT>
 function InputIsValid() {
 //Check for missing name
 if (document.frmMailingList.txtName.value == "") {
 alert("Please include your name.");
 document.frmMailingList.txtName.focus();
 return false;
 }

 // NEW: Check for hyperlinks in the name field
 const urlPattern = /(https?:\/\/[^\s]+|www\.[^\s]+)/gi;
 if (urlPattern.test(document.frmMailingList.txtName.value)) {
 alert("Hyperlinks are not allowed in the name field.");
 document.frmMailingList.txtName.focus();
 return false;
 }

 //Check for missing Email Address
 if (document.frmMailingList.txtEmail.value == "") {
 alert("Please include your email address!");
 document.frmMailingList.txtEmail.focus();
 return false;
 }

 //If it made it here, everything looks ok
 return true;
 }
 // -->
</SCRIPT>

Step 2: Full Integrated Code

Here’s the complete code with both your original non-empty validation and the new hyperlink blocking logic combined:

<SCRIPT LANGUAGE=JAVASCRIPT>
 function InputIsValid() {
 //Check for missing name
 if (document.frmMailingList.txtName.value == "") {
 alert("Please include your name.");
 document.frmMailingList.txtName.focus();
 return false;
 }

 // Check for hyperlinks in the name field
 const urlPattern = /(https?:\/\/[^\s]+|www\.[^\s]+)/gi;
 if (urlPattern.test(document.frmMailingList.txtName.value)) {
 alert("Hyperlinks are not allowed in the name field.");
 document.frmMailingList.txtName.focus();
 return false;
 }

 //Check for missing Email Address
 if (document.frmMailingList.txtEmail.value == "") {
 alert("Please include your email address!");
 document.frmMailingList.txtEmail.focus();
 return false;
 }

 //If it made it here, everything looks ok
 return true;
 }
 // -->
</SCRIPT>
<form name="frmMailingList" action="addMailingList.asp" method="post" onSubmit="return InputIsValid()">
 <table>
 <tr>
 <td height="26" colspan="2" align=right><strong><font face=arial size=-1>Name </font></strong></td>
 <td colspan="2"><input name="txtName" size=30 maxlength="35"> </td>
 </tr>
 <tr>
 <td height="26" colspan="2" align=right><strong><font face=arial size=-1>Email </font></strong></td>
 <td colspan="2"><input name="txtEmail" size=30> </td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 </tr>
 <tr>
 <td height="28" align=right>&nbsp;</td>
 <td align=right>&nbsp;</td>
 <td colspan="4" >
 <input type="hidden" name="Action" value="Go">
 <input type=submit name=btnAdd value="Submit"> &nbsp;
 <input id=reset name=reset type=reset value="Clear form">
 </td>
 </tr>
</table>
<br>
</form>

Quick Notes:

  • The regex /(https?:\/\/[^\s]+|www\.[^\s]+)/gi catches most common URL formats (HTTP/HTTPS links and www. prefixed links)
  • We keep the validation flow logical: first check for empty input, then check for invalid content
  • If a hyperlink is detected, the user gets an alert, the name field is focused, and form submission is blocked—just like your existing non-empty checks

内容的提问来源于stack exchange,提问作者Misty Roberts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 10:48:10