Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

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:', '');
0
Joana
Asked:
Joana
  • 5
  • 4
  • 2
  • +1
1 Solution
 
cirtapCommented:
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
 
JoanaAuthor Commented:
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
 
cirtapCommented:
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
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!

 
JoanaAuthor Commented:
Thanks Heaps :-)
0
 
walburyCommented:
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
 
voskCommented:
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
 
voskCommented:
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
 
cirtapCommented:
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
 
JoanaAuthor Commented:
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
 
JoanaAuthor Commented:
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
 
cirtapCommented:
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
 
JoanaAuthor Commented:
Thank you both:-)  (Site requirmenets are that they have to have JScript on - yay)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now