Solved

Check Form Input For Invalid Characters

Posted on 2004-04-23
5
656 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

839 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