?
Solved

Prompt Box Validation

Posted on 2003-03-05
12
Medium Priority
?
394 Views
Last Modified: 2008-03-17
I have the below prompt box.  I want user only to be able to enter in a-z or 0-9 not . ? ! # < > etc..

How do i do this

var sTitle = window.prompt('Please enter the new title:', '');
0
Comment
Question by:Joana
[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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:cirtap
ID: 8076337
Hi Joana,

you can't with just the prompt.
You must accept what the user enters and then use a regular expression to validate the return value. If the validation fails, ask again and again...
You may put this in a conditional loop, but keep the condition to something like a counter that increments upon each failure.

here's a dummy code
cnt=0
while (cnt < 5) { // loop 5 times max.
   ret = prompt   // get value from user
   RegExp ret     // validate value
   if RegExp==ok break;  // in case its ok, leave loop
   cnt++;         // incr. counter
}

CirTap
0
 

Author Comment

by:Joana
ID: 8076397
Sorry i don't really get what i am meant to do.  (it also has to work in NS 4.7 IE 4.0.1 and the Mac)

My Function :

          // get a new title if required
          if (sTag == 'retitle') {
               var sTitle = window.prompt('Please enter the new title:', '');

               if (sTitle == null || sTitle == '') {
                    return
               }
               
               cnt=0
               while (cnt < 5) { // loop 5 times max.
               ret = prompt   // get value from user
               RegExp ret     // validate value
               if RegExp==ok break;  // in case its ok, leave loop
                cnt++;         // incr. counter
}
               
               sURL = sURL + '&Title=' + sTitle
          }
0
 
LVL 5

Expert Comment

by:cirtap
ID: 8077133
hmmm,
not sure if IE4 already knows regular expressions...
actually the code I gave you is a dummy, not working.
I'll figure out the regular expression part and come back in a couple of hours (its 4AM here; can't think anymore <g>).
maybe some other expert knows what I mean and provides the code in the meantime.


CirTap
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:Joana
ID: 8077158
Thanks Heaps :-)
0
 
LVL 1

Accepted Solution

by:
walbury earned 200 total points
ID: 8077339
Hi ya,

AS far as I know below should work on a Mac as it's fairly dated javascript;

function stripit(){
    //USER input    
    var s1=document.forms[0].text1.value;
    //chars to KEEP
    var s2=document.forms[0].txtOKchars.value;
    //result
    document.forms[0].text2.value=removeChars(s1,s2);
}    
function removeChars(strIn, charList){
var sOut = '';
for (iPos=0;iPos < strIn.length;iPos++) {    
    if (charList.indexOf(strIn.charAt(iPos))>-1)
     sOut = sOut+strIn.charAt(iPos);              
    }
    return sOut;
}
I haven't included the form code as it should be fairly easy to mock-up. The main thing to know is that the value of txtOKchars is set to "abcdefghijklmnoprstuvwxyz0123456789".
(of course this can be changed as per requirements)

Nothing fancy but it does the job!
w.
0
 
LVL 3

Expert Comment

by:vosk
ID: 8078378
Maybe I'll say a strange thing, but why you use a predefined prompt? Easily you can create something like a prompt, and use onKeyPress event to check wich keys are entering the user. You can use a function to get and store the last correct value and return that value when the user enters a no-valid key.
jbosch(vosk)
0
 
LVL 3

Expert Comment

by:vosk
ID: 8078452
I mean something like that:
<html>
<head>
<script language="JavaScript">
var pos = 0, last;
function A()
{
var a = document.a.b.value.substring(0,pos);
var b = document.a.b.value.substr(pos);
if (b == "?" || b == "!" || b == "#" || b == "<" || b == ">" || b == "¡")
     {alert("no valid key")
      document.a.b.value = a;
     };
else
     {pos++};
}
</script>
</head>
<body>
<table id="prompttable" width="40%">
<form name="a">
<input type="text" name="b" value="" onKeyUp="A();">
<input type="button" name="c" value="FINISH">
</form>
</table>
</body>
</html>
Of course that you have to modify the table and made it more similar to a prompt, and also you have to implement the list of keys that you don't let enter.
jbosch(vosk)
0
 
LVL 5

Expert Comment

by:cirtap
ID: 8080044
Hi Joana,
I'd support Walbury's answer as it doesn't not really require another form, which may interfere with the current design of the page, AND more important: MSIE 4 does not have regular expressions so my first idea would not work anyways.

So his "dated" script is a very good and simple solution 'cos it's working and it's him to give the points :)

I'd change the form stuff into variables, which would make it look like this: (same as Walbury's but no FORM involved)

// this may go into the global part of your <script>
var goodChars="abcdefghijklmnoprstuvwxyz0123456789";
function stripit(BadText){
   return=removeChars(BadText,goodChars);
}    
function removeChars(strIn, charList){
var sOut = '';
for (iPos=0;iPos < strIn.length;iPos++) {    
   if (charList.indexOf(strIn.charAt(iPos))>-1)
    sOut = sOut+strIn.charAt(iPos);              
   }
   return sOut;
}

// this is inside your "validation" routine
if (sTag == 'retitle') {
  var sTitle = window.prompt("Please enter the new title:\nCharacters other than a-z, A-Z, and 0-9 will be rejected", '');
  sTitle = stripit(sTitle);
  sURL = sURL + '&Title=' + sTitle;
}

I added the "valid chars" hint for the user. You should always tell the user what's possible.

CirTap
0
 

Author Comment

by:Joana
ID: 8083157
Hiya Guys I use the functions you guys gave me (without forms) and it does weird things like if i type in

Joana it give the title oana
It lets me put in illegal characters
0
 

Author Comment

by:Joana
ID: 8083332
Sorry just relised what it is doing
it is removing capital letters
Do i need to put the capital letters into teh string or is there a way to make it ignore case

vosk:  i don't know sorry.  I am new to this so just do as i am told which was to use a prompt box - lol.  
0
 
LVL 5

Expert Comment

by:cirtap
ID: 8088059
Joana,

you're right with the caps. To keep things easy, just add them to the list of valid characters.
You may as well change any of the functions to work with upperCase, but this will get things a 'bit' more complicated.
The goodChars string is a very straightforward and easy to extend. The only drawbacks it has is, it required JavaScript to be active and it constantly runs through every character in the goodChars-string for every character in the string you want to check, so this may not receive an academy award for optimized programming, but it works in all JS-aware browsers - and that's what counts, right?
Just keep in mind, that if someone uses your page w/o JavaScript, it'll fail.

Have fun
CirTap
0
 

Author Comment

by:Joana
ID: 8098617
Thank you both:-)  (Site requirmenets are that they have to have JScript on - yay)
0

Featured Post

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

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

764 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