Solved

Invalid Character Entered Error Works in MSIE but not Netscape

Posted on 2004-05-01
6
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

729 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