PDA

View Full Version : Block string in form


win555
01-26-2003, 05:30 PM
Hi I need a script that does not let a user type certain words, I need to block the word 'snap' and 'telephone' from being writen in the input field...

Any ideas?...
Thanks

Jimbo
01-26-2003, 05:47 PM
<html>
<head>
<title>Untitled</title>
<script langauge="text/javascript">
<!--
function check_it()
{
boxs = document.the_form.the_text.value
if (boxs == "snap" || boxs == "telephone")
{
boxs = ""
alert("Cannot type snap or telephone")
return false
else
{return true}
}
}
-->
</script>
</head>

<body>

<form name="the_form" onSubmit="return check_it()">
<input name="the_text" type="text">
<input name="submit_it" type="submit">
</form>

</body>
</html>






try using that

Jimbo
01-26-2003, 05:51 PM
<script langauge="text/javascript">
<!--
function check_it()
{
boxs = document.the_form.the_text.value
if (boxs == "snap" || boxs == "telephone")
{
boxs = ""
alert("Cannot type snap or telephone")
return false
}
else
{return true}
}
-->
</script>


The script above is messed up, here it is

win555
01-26-2003, 06:01 PM
Thanks it works great for the words only, but now the interesting part , what if it contains the word in within, what if they type 'snapphone'...

I need to block any word that contains the string 'snap'...

Any ideas..., Thanks for the help...

win555
01-26-2003, 06:03 PM
I think we can close this one...

Check this out:

<script langauge="text/javascript">
//list of banned words
var bannedWords = new Array (
"snap",
"telephone",
"banana"
);

function checkForm (form) {
// loop on all elements in the form
for (var i = 0; i < form.elements.length; ++i) {
var elem = form.elements[i];

// if this element is not a text field, skip it
if (elem.type != 'text') continue;

// search for every banned word using "indexOf"
for (var j = 0; j < bannedWords.length; ++j)
if (elem.value.indexOf (bannedWords[j]) >= 0) {
alert ('"' + bannedWords[j] + '" is not an allowed word.');
return false;
}
}

// if we got this far then it means we didn't find any banned words
return true;
}
</script>

</head>

<body>

<form name="the_form" onsubmit="return checkForm (this);">
<input name="the_text" type="text">
<input name="submit_it" type="submit">
</form>

</body>
</html>

win555
01-26-2003, 06:26 PM
Ok one problem do, I need to work only on 1 input field and is afecting all the form.

I tryed this but is not working:

for (var i = 0; i < document.Register.UserLogin.elements.length; ++i) {
var elem = document.Register.UserLogin.elements[i];

Any ideas please...

whammy
01-28-2003, 12:22 AM
<html>
<head>
<script type="text/javascript">
<!--
function checkBannedWords(f) {
return !/telephone|snap/g.test(f);
}
// -->
</script>
</head>
<body>
<form onsubmit="return checkBannedWords(this.blah.value)">
<input type="text" name="blah" />
<input type="submit" value="Submit" />
</form>
</body>
</html>