Link to home
Start Free TrialLog in
Avatar of Joana
Joana

asked on

Prompt Box Validation

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:', '');
Avatar of Member_2_547613
Member_2_547613
Flag of Germany image

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
Avatar of Joana
Joana

ASKER

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
          }
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
Avatar of Joana

ASKER

Thanks Heaps :-)
ASKER CERTIFIED SOLUTION
Avatar of walbury
walbury

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
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)
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
Avatar of Joana

ASKER

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
Avatar of Joana

ASKER

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.  
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
Avatar of Joana

ASKER

Thank you both:-)  (Site requirmenets are that they have to have JScript on - yay)