Forum Moderators: open

Message Too Old, No Replies

validate - check for "#" sign

avoid entering a #

         

white300z

7:54 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



function Validat(theForm)
{

if (theForm.filename.value == "#")
{
alert("Please take the # sign out of the filename field.");
theForm.filename.focus();
return (false);
}

return (true);
}
//--></script>

In the form:

<input TYPE="FILE" NAME="filename" size="20">

Basically, what the above does is check for there ONLY being a # in the field. How can I check to make sure what is entered does not CONTAIN a "#"?

Thanks,

Jim

j4mes

8:05 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



Hi.

You could try using indexOf instead, like this:

<script type="text/javascript">

function Validate()
{

var hash = theForm.filename.value.indexOf("#");

if (hash != -1)
{
alert("Please take the # sign out of the filename field.");
theForm.filename.focus();
return (false);
}

return (true);
}

</script>

<form name="theForm">
<input type="text" name="filename" />
<input type="button" value="click" onclick="Validate()" />
</form>

Then the # can be anywhere!

Good luck :)

white300z

8:32 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



Thanks j4ames!

Someone also suggested I try using

if (theForm.filename.value.match(/#/))

One of those should work. So I'll probaby give both a try.

Thanks again!

Bernard Marx

10:30 pm on Jun 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not?:

theForm.filename.value = theForm.filename.value.replace(/#/g,"")

You won't have to either check for its existence, or ask anyone to remove it if it's there.

j4mes

11:22 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



Cunning :)

Bernard Marx

11:23 pm on Jun 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Liked that stunt, did you?

j4mes

10:27 am on Jun 11, 2004 (gmt 0)

10+ Year Member



lol :P

white300z

1:25 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



I thought about using a replace, however this particular field is for a file upload and it is the filename, so I'd rather the visitor know that they have to rename their file and do so before they upload it instead of having the page rename it for them and the filename appears different from what they had originally thought. I may filter out other characters later on, but at least this satisfies the person who wanted the functionality.
Thanks for your help, I used

if (theForm.filename.value.match(/#/))

Jim