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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating fields on a form

Status
Not open for further replies.

n0795

Programmer
Jul 30, 2001
136
US
I use this code to validate one field on a form.
I have two questions 1. is how do i change this so instead of checking text lenght it checks for Bad words IE:cat dog horse and doesnt let then enter any of the words in the list.
My next question is this will check one field at the moment it checks "NAME" lets say i want this one script to check two fields how would i change it? and if i wanted to check the lenght of this field and check if there was any bad words in the same fields.
I assume this would be 2 scripts but the ONSUBMIT only checks one script.
<script language=&quot;javascript&quot;>
<!---

function checklength(Daform)
{
var strName = form.Name.value;

if (strName.length < 3)
{
alert('you must enter at least 3 characters.');
return false;
}

return true;
}
//-->
</script>
onSubmit='return checklength(this);'
any help would be great.
 
Hi n075,

Try this sample:

<script language=&quot;javascript&quot;>
function checkAll() {
var myBadWords = new Array(3), myValue = &quot;&quot;, myValue_2 = &quot;&quot;,i = 0;

myBadWords[0] = &quot;cat&quot;;
myBadWords[1] = &quot;dog&quot;;
myBadWords[2] = &quot;horse&quot;;

myValue = &quot;&quot; + document.forms[0].name.value;
myValue_2 = &quot;&quot; + document.forms[0].name_2.value;
if (myValue_2.length < 3) {
alert('you must enter at least 3 characters.');
return false;
}
for (i = 0; i < myBadWords.length; i++) {
if (myBadWords == myValue || myBadWords == myValue_2) {
alert('This word is in the black list');
return false;
}
};
return true;
};
</script>

Hope this helps...
 
Not sure if im doing this right in this code i do not need to check for 3 characters or less&quot;My bad&quot;.
But i do have a piece of code

<script LANGUAGE=&quot;JavaScript&quot;>
<!-- HIDE FROM OTHER BROWSERS
function validate()
{
if (add.title.value == &quot;&quot;)
{
alert(&quot;You must give your Review a title!&quot;);
add.title.focus();
return (false);
}

return (true);
}
and onsubmit is onsumit=&quot;return validate(this)&quot;>

How do i keep this and still use your code for bad words.
The other code only needs to validate one field yet your code i need to validate all fields.
can you onsubmit to more than one thing&quot;If you understand what i mean&quot;.
Help Help Help
thanks
 
No, you can't onSubmit two things but you can call a function from the first one...
 
U could use the function on &quot;onblur&quot; event on to your textbox

<input name=mytxt id=mytxt onblur=&quot;doCheck();&quot;>
<script language=&quot;javascript&quot;>
function doCheck()
{
//put your code here
}
</script>

if u need more please tell me...
________

George
 
How do i change this so it olny checks the field discription for bad words.
Also lets say i want to check the whole page for any instance of a bad word how would i do that.
Right now this has some code to check for 3 characters entered i dont need that on this form.
<textarea name=&quot;discription&quot; cols=&quot;50&quot; rows=&quot;5&quot; id=discription onblur=&quot;doCheck();&quot;></textarea>
<script language=&quot;javascript&quot;>
function doCheck()
{
var myBadWords = new Array(3), myValue = &quot;&quot;, myValue_2 = &quot;&quot;,i = 0;

myBadWords[0] = &quot;cat&quot;;
myBadWords[1] = &quot;dog&quot;;
myBadWords[2] = &quot;horse&quot;;

myValue = &quot;&quot; + document.forms[0].name.value;
myValue_2 = &quot;&quot; + document.forms[0].name_2.value;
if (myValue_2.length < 3) {
alert('you must enter at least 3 characters.');
return false;
}
for (i = 0; i < myBadWords.length; i++) {
if (myBadWords == myValue || myBadWords == myValue_2) {
alert('This word is in the black list');
return false;
}
};
return true;
};
</script>
 
I have tried to use this and i get an error expected object.
Also i assume this check just the field discription.
Lets say i want to check the field discription and the field name how would i do that
<textarea name=&quot;discription&quot; cols=&quot;50&quot; rows=&quot;5&quot; id=discription onblur=&quot;checkAll();&quot;></textarea>
<script language=&quot;javascript&quot;>
function checkAll() {
var myBadWords = new Array(3), myValue = &quot;&quot;, myValue_2 = &quot;&quot;,i = 0;

myBadWords[0] = &quot;cat&quot;;
myBadWords[1] = &quot;dog&quot;;
myBadWords[2] = &quot;horse&quot;;


}
for (i = 0; i < myBadWords.length; i++) {
if (myBadWords == myValue || myBadWords == myValue_2) {
alert('This word is in the black list');
return false;
}
};
return true;
};
</script>
 
Hi n0795,

You have to take away the || myBadWords == myValue_2

Hope this helps...
 
U have to access all <input> tags or what u need (<textarea>...) and verify the value of all an put an message to alert the user u could use this code...

<HTML>
<BODY onload=&quot;doTags()&quot;>
<input name=&quot;i1&quot; id=&quot;i1&quot; value=&quot;v1&quot;>
<input name=&quot;i2&quot; id=&quot;i2&quot; value=&quot;v2&quot;>
<input name=&quot;i3&quot; id=&quot;i3&quot; value=&quot;v3&quot;>
<P>&nbsp;</P>

</BODY>
<script language=&quot;javascript&quot;>
function doTags()
{
var inputags=document.all.tags(&quot;input&quot;)
for(i = 0; i < inputags.length; i++)
{
checkvalue(inputags(i));
};
};
function checkvalue(elem)
{
alert('\&quot;'+elem.tagName+'\&quot; element with name \&quot;' +elem.name+'\&quot; has an value \&quot;'+elem.value+'\&quot;');
};

</script>
</HTML>
________

George
 
Im now getting an error &quot;Return statement outside of function&quot; with this code ....
Any ideas???
<script language=&quot;javascript&quot;>
function checkAll() {
var myBadWords = new Array(3), myValue = &quot;&quot;,i = 0;

myBadWords[0] = &quot;cat&quot;;
myBadWords[1] = &quot;dog&quot;;
myBadWords[2] = &quot;horse&quot;;


}
for (i = 0; i < myBadWords.length; i++) {
if (myBadWords == myValue) {
alert('This word is in the black list');
return false;
}
}; <----&quot;I have tried taking this out and its the same error&quot;
return true;
};
</script>

<textarea name=&quot;discription&quot; cols=&quot;50&quot; rows=&quot;5&quot; id=discription onblur=&quot;checkAll();&quot;></textarea>
 
U have an error on script cuz u close the function body before the return statments...

this the right body

<script language=&quot;javascript&quot;>
function checkAll() {
var myBadWords = new Array(3), myValue = &quot;&quot;,i = 0;

myBadWords[0] = &quot;cat&quot;;
myBadWords[1] = &quot;dog&quot;;
myBadWords[2] = &quot;horse&quot;;

//here was } the error
for (i = 0; i < myBadWords.length; i++) {
if (myBadWords == myValue) {
alert('This word is in the black list');
return false;
};
};
return true;
};
</script>

<textarea name=&quot;discription&quot; cols=&quot;50&quot; rows=&quot;5&quot; id=discription onblur=&quot;checkAll();&quot;></textarea>
________

George
 
Ok I changed the code ..
I do not get any errors on the page but it doesnt work
it lets me put in
&quot;the cat and the horse&quot;
or just &quot;cat&quot; here is my code now...
<script language=&quot;javascript&quot;>
function checkAll() {
var myBadWords = new Array(3), myValue = &quot;&quot;,i = 0;

myBadWords[0] = &quot;cat&quot;;
myBadWords[1] = &quot;dog&quot;;
myBadWords[2] = &quot;horse&quot;;


for (i = 0; i < myBadWords.length; i++) {
if (myBadWords == myValue) {
alert('This word is in the black list');
return false;
};
};
return true;
};
</script>


<textarea name=&quot;discription&quot; cols=&quot;50&quot; rows=&quot;5&quot; id=discription onblur=&quot;checkAll();&quot;></textarea>

Any help would be great....
This is driving me crazy
 

this should solve all your problem...

<textarea name=&quot;discription&quot; cols=&quot;50&quot; rows=&quot;5&quot; id=discription onblur=&quot;checkAll(this);&quot;></textarea>

<script language=&quot;javascript&quot;>
var myBadWords = new Array(3), myValue = &quot;&quot;;

myBadWords[0] = &quot;cat&quot;;
myBadWords[1] = &quot;dog&quot;;
myBadWords[2] = &quot;horse&quot;;

function checkAll(elem)
{
var re;
var myTextAreaValue=&quot;&quot;;//forced to be an string object not anything else
myTextAreaValue=elem.value;
for (i = 0; i < myBadWords.length; i++)
{
re=eval(&quot;/&quot;+myBadWords+&quot;/i&quot;);
//search
//-1 if the string do not exist
//position of the substring in the main string
if (myTextAreaValue.search(re)!=-1)
{
alert('This word is in the black list -&quot;'+myBadWords+'&quot;');
elem.focus();
};
};
};
</script>
________

George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top