Jump to content

Regexp


jarrett000

Recommended Posts

Hello again, I'm having just a bit of a problem with a RegExp function that dosen't work. The goal of the function is to redirect and alert the user if the information they put into the text box contained "<". Reason being I want to prevent the use of HTML in that text box. Here's the java script:

function RegExp() {				var Expression = new RegExp("<");				var Location = document.form.enterN.value;				var Test = Expression.test(Location);				if (Test=="true") {				var r = top.location="http://www.google.com/";				alert("Your name may not contain < (less than symbol). Please re-enter your name without it.");				}}

And here's the form:

<form method='get' id='form'><p id='black'>Name</p><input type='text' id='enterN' class='Feild' /> <input type='submit' value='Enter' class='Feild' onclick='RegExp()' /></form>

Any help at all would be appreciated

Link to comment
Share on other sites

Your script has quite a few errors.

function RegExp() {

RegExp is already a reserved word.

var Location = document.form.enterN.value;

It's highly preferrable that you access the element's value like this: document.getElementById("enterN").value;

if (Test=="true") {

Javascript is probably forgiving about this, but in booleans, true and false do not go between quotes.

var r = top.location="http://www.google.com/";

This line is going to redirect the page immediately and ignore any code that follows. I don't know why you are assigning it to r, either. You can just type top.location = "http://www.google.com/".Finally, you most likely can just fix this on the server-side by using the htmlentities() PHP function (or any equivalent in other server-side languages). Validating the form field only in Javascript isn't secure. People can deactivate Javascript.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...