Solved

Invalid Character Entered Error Works in MSIE but not Netscape

Posted on 2004-05-01
6
309 Views
Last Modified: 2010-08-05
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)"
0
Comment
Question by:lserrano
  • 3
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
Lord_McFly earned 125 total points
ID: 10970158
I'm not sure what you mean when you say 'but the resulting page appears' - I can see how once your script has performed the validation it can possibly move onto another page.

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="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var newInput="";

            for(i=0;i<Input.value.length;i++)
                  {
                        chck=Input.value.charAt(i);
                        
                        if(control.indexOf(chck,0)!=-1)
                              {
                                    newInput+=chck;
                              }
                  }
            
            Input.value = newInput;
      }


<input type="text" onKeyUp="ValidateKeyStroke(this)">
0
 

Author Comment

by:lserrano
ID: 10974099
Lord_McFly, I wonder if you can expand of this so that if the user enters an unrealistic name which does not contain any vowels (i.e., a,e,i,o, OR u) such as dffdffsdsfdfs, then the script will erase what he has entered and do not pass the form until he has entered a word which contains at least 1 vowel.
0
 
LVL 6

Expert Comment

by:Lord_McFly
ID: 10974205
Ok I couldn't do it in the same function so I've done the validation when the form is submitted. If the text lacks any vowels it will display an alert but won't allow the page to submit.

<html>
<head>
<title>Example</title>
<script language="JavaScript">
<!--
function ValidateKeyStroke(Input)
      {
            var control="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var newInput="";

            for(i=0;i<Input.value.length;i++)
                  {
                        chck=Input.value.charAt(i);
                        
                        if(control.indexOf(chck,0)!=-1)
                              {
                                    newInput+=chck;
                              }
                  }
            
            Input.value = newInput;
      }

function CheckForm(Form)
      {
            
            var control="aeiouAEIOU"
            var chck
            var i
            var ok = false
            
            for(i=0; i<Form.Name.value.length; i++)
                  {
                        chck=Form.Name.value.charAt(i);

                        if(control.indexOf(chck,0)!=-1)
                              {
                                    ok = true
                              }
                  }
            
            if(ok==false)
                  {
                        alert("Name seems invalid")
                  }
                  
            return ok
      }

//-->
</script>
</head>
<body>
<form name="Data" method="post" action="../ASP/UsingDATEDIFF.asp" onSubmit="return CheckForm(this)">
      <input type="text" name="Name" onKeyUp="ValidateKeyStroke(this)" value="">
</form>
</body>
</html>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lserrano
ID: 10974980
Hmm... your code seems to work awesome as standalone, but when I add it to my existing page (http://www.worldwidelists.com/cgi-bin/lists.cgi?popup.test) and submit, I do not get the alert box. Any ideas why?

By the way, thank you so much for all your help!

L
0
 
LVL 6

Expert Comment

by:Lord_McFly
ID: 10976612
I had a look at the page - I noticed the name of the textbox is 'namex' - this means that the name in the function 'CheckForm(Form)' needs to be changed...

for(i=0; i<Form.namex.value.length; i++)      <------ notice change.
0
 

Author Comment

by:lserrano
ID: 10977898
Super! Thanks for all your help Lord_McFly! ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now