I am very frustrated because I have spammers who continue to fill out a form to be added to a mailing list with hyperlinks. I want to end this. I have input validation for my form that won't let any fields be blank. The input validation works great. But I have not been able to figure out how to add validation that would not allow any hyperlinks.

I found this code that works by itself and would like to incorporate it into the code for my form that already has input validation, but I have not been able to figure out how to incorporate it.

<form id="myForm"> <input type="text" id="myInput"> <button type="submit">Submit</button> </form> <script> document.getElementById('myForm').addEventListener('submit', function(event) { const input = document.getElementById('myInput'); // Simple regex to detect common URL patterns const urlPattern = /(https?:\/\/[^\s]+|www\.[^\s]+)/gi; if (urlPattern.test(input.value)) { alert("Hyperlinks are not allowed."); event.preventDefault(); // Stop the form from submitting } }); </script>

Here is my code for the form that won't allow any missing fields below. I need help with incorporating a code that would not allow hyperlinks for the txtName (I already have code that will not let it be blank).

<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 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()"> <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>

Misty Roberts's user avatar

3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.