Solved

Invalid Character Entered Error Works in MSIE but not Netscape

Posted on 2004-05-01
6
307 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now