Solved

Check Form Input For Invalid Characters

Posted on 2004-04-23
5
653 Views
Last Modified: 2008-03-10
I can't get this to work. what went wrong?

<SCRIPT>
function checkSearch(input)
{
var value = true;
var notValidChars = /[^a-z0-9]/i;

if( notValidChars.test(input.value) )
{
alert("Please do not use invalid symbols!");
input.focus();
value = false;
}
return value;
}
</SCRIPT>

<body>
<form method="post" action="/cgi-bin/check.cgi" onsubmit="return checkSearch(this.Search)">
<p>&nbsp;</p>
<p><input type="text" name="T1" size="20"></p>
<p><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
0
Comment
Question by:Cleavis
  • 3
  • 2
5 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10898690
Try this

<SCRIPT>
function checkSearch(input)
{
var value = true;
var notValidChars = /[^a-z0-9]/i;

if( notValidChars.test(input.value) )
{
alert("Please do not use invalid symbols!");
input.focus();
value = false;
}
return value;
}
</SCRIPT>

<body>
<form method="post" action="/cgi-bin/check.cgi" onsubmit="return checkSearch(this.T1)">
<p>&nbsp;</p>
<p><input type="text" name="T1" size="20"></p>
<p><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
0
 

Author Comment

by:Cleavis
ID: 10898720
that works for T1.

is there a way to check all input fields in the form?
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 500 total points
ID: 10898757
<SCRIPT>
function checkSearch(form){
var notValidChars = /[^a-z0-9]/i;
for (i=0;i<form.elements.length;i++)
 if( notValidChars.test(form.elements[i].value) )
 {
  alert("Please do not use invalid symbols!");
  form.elements[i].focus();
  return false;
 }
return true
}
</SCRIPT>

<body>
<form method="post" action="/cgi-bin/check.cgi" onsubmit="return checkSearch(this)">
<p>&nbsp;</p>
<p><input type="text" name="T1" size="20"></p>
<p><input type="text" name="T2" size="20"></p>
<p><input type="text" name="T3" size="20"></p>
<p><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
0
 

Author Comment

by:Cleavis
ID: 10898794
Thank You.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10898891
Thanks for the points, GfW
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now