Click to See Complete Forum and Search --> : Validate form - exclude characters


croft
07-14-2003, 11:32 AM
Hi everybody!

I would appreciate if you could help me out:

I need to disallow "<" and ">" characters in the input form.

I found many scripts how to allow numeric or alphanumeric values. I tried to mimic with "<>" but it din't work!

Please help, a sample code would be great!

Thanks so much.

Croft.

requestcode
07-14-2003, 11:59 AM
You can use a regular expresion to do that. Here is a small example:
<html>
<head>
<title>Regulare Expressions</title>
<script language="JavaScript">

re1=/[<||>]/g
function checkval()
{
myvar1=document.myform.num1.value
myvar2=myvar1.replace(re1," ")
document.myform.num1.value=myvar2
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="num1" size="11" maxlength="11">
<input type="button" value="Enter something" onClick="checkval()">
</form>
</body>
</html>
The above will replace them with a space. If you want to replace them with any thing else then you would modify this line:myvar2=myvar1.replace(re1," ")
Put the character you want to replace them with inside the double quotes. If you want to remove them then just remove the space between the double quotes.

croft
07-14-2003, 03:55 PM
.

Charles
07-14-2003, 04:40 PM
1) The "language" attribute was depricated back in 1997.

2) That will replace three characters, '<', '>' and '|'.

3) We can shorten that up quite a bit.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
-->
</style>
<title>Example</title>
<form action="">
<div>
<label><input type="text" onchange="this.value = this.value.replace(/[<>]/g, '*')">Foo</label>
<button type="submit">Submit</button>
</div>
</form>