The following script shows the user an alert message if he enters a dash (-) in the input field. This works nicely in MSIE, but not in Netscape. Can someone make this work in both browsers? Actually, the alert box appears in netscape nicely, but the resulting page appears. I should not be take to the next page unless I have entered a lowercase letter(s) or UPPERCASE letter(s):
<script>
function validate(field) {
//var valid = "abcdefghijklmnopqrstuvwxyz0123456789"
var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Name is invalid (numbers and special characters are not allowed)");
field.focus();
field.select();
}
}
</script>
<input type="text" name="namex" onBlur="validate(this)"
I offer a slightly different script which will not allow unwanted characters whcih means when the page does move on the field won't contain invalid characters.
function ValidateKeyStroke(Input)
{
var control="abcdefghijklmnopq
var newInput="";
for(i=0;i<Input.value.leng
{
chck=Input.value.charAt(i)
if(control.indexOf(chck,0)
{
newInput+=chck;
}
}
Input.value = newInput;
}
<input type="text" onKeyUp="ValidateKeyStroke