Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I detect whether a user has input script into a text box?

Status
Not open for further replies.

andyhaywood

Programmer
Jan 12, 2004
4
GB
Hi,

Does anyone know whether it's possible to detect whether a user has put some form of script in a text input? Basically we need to stop people inputting something like "<script>alert(document.cookie)</script>" into an input as when this is returned from the server in xml form it is causing errors.

Thanks.
 
this is something i suggest doing server-side.

however, if you want to perform an initial check client-side, a regular expression should get you what you need. is it only <script> tags you're trying to exclude?



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Whatever is input is sent to the server and returned later as xml which is translated with xsl like this:

<input type="text" ...>
<xsl:value-of select="..."/>
</input>

So anything in that user value is put in here which I'm guessing means we need to test for more than just <script> tags?
 
What if you call escape or encode (I've forgotten the exact function name, sorry), to change your string into something XML legal?

"<script>alert(document.cookie)</script>" would become
"&gt.script&lt.alert(document.cookie)&gt./script&lt."

 
hi andy,

sorry for the delay. i've put together a sample of something that may work for you. let me know what you think:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--

function removeStuff(t) {
    var re = /\<.+?\>/g;
    t.value = t.value.replace( re, "" );
}

//--></script>
</head>

<body>

<form name="f">
<input type="text" onchange="removeStuff(this);" name="t" />
</form>

</body>
</html>



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
What if you call escape or encode (I've forgotten the exact function name, sorry), to change your string into something XML legal?

it's escape() & unescape()

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I think an easy way to do it just test the <script> tag, if you just want the user to change it

if (value.match(/^.*\<script\>.*$/)) return false;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top