?
Solved

Invalid Character Entered Error Works in MSIE but not Netscape

Posted on 2004-05-01
6
Medium Priority
?
315 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 500 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

777 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