Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 21st, 2005, 12:50 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default statement outside of function

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=o; i<invalidChars.length; i++)
{
if (testField.indexOf(invalidChars.charAt(i),0) > -1)
{return false;}
}
</script>
</head>

<body>
<form onSubmit="return invalidChars();">
<input type="text" name="testField">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Whats wrong with my code???

I get:

Error:

'return' statement outside of function

www.crmpicco.co.uk
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
  #2 (permalink)  
Old February 21st, 2005, 12:54 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=o; i<invalidChars.length; i++)
{
if (agency.indexOf(invalidChars.charAt(i),0) > -1)
{return false;}
}

Its the Javascript thats giving me an error

Why?

www.crmpicco.co.uk
  #3 (permalink)  
Old February 21st, 2005, 01:07 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

I've now got this but still get error:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">
function removeBadCharacters(text)
{
   if (text.replace) {
      text.replace(/[<>\"\'%;\)\(&\+]/, '');
   }
   return string;
}


</script>

</head>

<body>
<form name="form">
<input type="text" name="text">
<input type="submit" name="submit" onClick="return removeBadCharacters();">
</form>
</body>
</html>


www.crmpicco.co.uk
  #4 (permalink)  
Old February 21st, 2005, 01:29 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

http://javascript.internet.com/forms/val-char.html

www.crmpicco.co.uk
  #5 (permalink)  
Old February 22nd, 2005, 01:08 AM
Friend of Wrox
 
Join Date: Dec 2004
Location: Chennai, Tamil nadu, India.
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

What is your actual problem? By the way do you face that "problem" still?

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
  #6 (permalink)  
Old February 22nd, 2005, 04:28 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Your first problem was exactly what the error message said, you can't return a value from a function if you are not in a function, where would it return it to?
I don't know what erreor message you are getting in the latest version but "string" is not defined in the code shown. You may want:
Code:
function removeBadCharacters(text) 
{
   if (text.replace) {
      return text.replace(/[<>\"\'%;\)\(&\+]/g, '');
   }
}

--

Joe (Microsoft MVP - XML)


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help writing SQL Statement/ .net code for function carswelljr Classic ASP Databases 2 August 24th, 2006 03:31 PM
if statement and contains function richjo100 XSLT 6 September 27th, 2004 05:04 AM
retreive function/Line from macro or function? MikoMax J2EE 0 April 1st, 2004 04:42 AM
Calling a Function within an MS SQL Statement flyin Classic ASP Databases 3 September 5th, 2003 03:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.