Click to See Complete Forum and Search --> : Javascript Functionality


asiamex
07-01-2003, 07:21 AM
Hi,

The JS may help me to prevent user from entering a '&' in a search box, but why does it pop an alert 'correct entry' rather than just submitting the form if the entry was correct? I tried modifying and taking out the alerts, but can't get it to submit the form if the entry is correct... Was it done this way just for the example?

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
function dodacheck(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
}
}
function doanothercheck(form) {
if(form.value.length < 1) {
alert("Please enter something.");
return false;
}
if(form.value.search(mikExp) == -1) {
alert("Correct Input");
return false;
}
else {
alert("Sorry, but the following characters\n\r\n\r@ $ % ^ & * # ( ) [ ] \\ { + } ` ~ = | \n\r\n\rare not allowed!\n");
form.select();
form.focus();
return false;
}
alert("Correct Input");
return false;
}
// End -->
</script>

</HEAD>

<form name=xyz onSubmit="return doanothercheck(this.txtTerm);">
<input type="text" name="txtTerm" size="35" maxlength="50" value="" onKeyUp="javascript:dodacheck(xyz.txtTerm);">
<br>
<input type=submit value=Check>
</form>

Charles
07-01-2003, 07:28 AM
It looks like your form never returns "true".

But you could simplify that a good bit by simply validating the field "onchange".

<form action=""">
<div>
<input type="text" name="txtTerm" size="35" maxlength="50" value="" onchange="if(/[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/.test(this.value) {alert(); this.value.replace(/[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/, ''); this.focus()">
<br>
<input type=submit value=Check>
</div>
</form>

asiamex
07-01-2003, 08:19 AM
I didn't understand your one, i tried this and it seems like it operating in reverse. the form submits only if there is a & in the form??? it shoul dbe the reverse and only alert when there is an ampersand present. am i correct?

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

function checkCharachter(myForm)

{

if
(/&/.test(myForm.Ampersand.value))
{return (true)}

alert("Invalid Charachter.")
return (false)}

// End -->
</script>

</HEAD>



<BODY>

<center>

<form action="" onSubmit="return checkCharachter(this)"><br>
<input type="text" name="Ampersand">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>


</center>

Charles
07-01-2003, 08:37 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<form action="">
<div>
<input type="text" onchange="this.ic = /&/; if (ic.test(this.value)) {alert('Ilegal Character or Characters'); this.value = this.value.replace(this.ic, ''); this.focus()}">
</div>
</form>

asiamex
07-01-2003, 09:00 AM
thank you for the help, that works great!