Solved

Invalid Character Entered Error Works in MSIE but not Netscape

Posted on 2004-05-01
6
311 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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…
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…

808 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