Link to home
Start Free TrialLog in
Avatar of simi
simi

asked on

regular expressions in java script

Please provide exact code to implement:
1. changing lower to upper char when entered by user
2. allow entering only digits
3. allow entering only letters and spaces

eg.

I have a text field and I need the code to put , say onkeyup .....

I need to simply block the user from entering unwanted chars, not to allow, then display an error messatge.
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Does this have to be on a key event?  I suggest you use the onblur event in the input element ...

1.  onblur="this.value = this.value.toLowerCase();"
2.  onblur="this.value = this.value.match(/^\d+$/);"
3.  onblur="this.value = this.value.match(/^[A-Z ]/i);"

Let me know if this has to be when the key is pressed.  That involves more script.  An alert can be added if needed but the code above will just remove any invalid characters.

bol

I left something out of the 3rd suggestion.

3.  onblur="this.value = this.value.match(/^[A-Z ]+/i);"

bol
Oops.  Hit submit too quick, forgot the other thing I left out.

3.  onblur="this.value = this.value.match(/^[A-Z ]+$/i);"
If you want to do it in an onkey event with an alert then the functions below go in the script tags in the head part of the html.  The event to call the function is below the functions.

funtion chkKeyLower(e) {
      var k = (e.keyCode)? e.keyCode : e.which;
      if (k < 97 || k > 122) {
            alert('Please use only lowercase characters.');
            return false;
      }
                 return true;
}
funtion chkKeyDigit(e) {
      var k = (e.keyCode)? e.keyCode : e.which;
      if (k < 48 || k > 57) {
            alert('Please use only digit characters.');
            return false;
      }
                 return true;
}
funtion chkKeyLetterSpace(e) {
      var k = (e.keyCode)? e.keyCode : e.which;
      var kName = String.fromCharCode(k);
      if (!kName.match(/^[A-Z ]+$/i)) {
            alert('Please use only alpha characters or a space.');
            return false;
      }
                 return true;
}

You would call any of the function with an onkey event like ...

onkeyup="return chkKeyLower(event);"

Let me know if you have a question.

bol
Avatar of simi
simi

ASKER

I want it on key up, so that each time an illegal char is beig entered, to be rejected, but the previous ones are lefe there.
I had the code but can't find it.
It is possible, and it id done on keyup.
My last post will do it then.  Use the function you need for 1, 2, or 3.  The example of the onkeyup event shows the 1st.

Let me know if you have a question.

bol
Avatar of simi

ASKER

It does not work.
I asked, for a working code, not hints.
even your keyword is funtion, so I had to correct it.
I clearly stated that I do not want behavior that pops up error messages, but simpl,y stops the char entered if not legal.
What your code does, egl for the chkKeyLower, it wrongfully pops up the message all the time, as it appears that the comparison of what number each char is mapped to is not correct, and it still allows the char to remain in the text field.
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

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

ASKER

Perhaps the comma confused you , I stated that I do not want to allow the llegal char entered then display an error messaege.
Anyway, I found the solution.
So I will delete the question.
Your suggestion does not work in certain scenarios, mainly because it uses the onkeydown event.
I have no time to go into details.
My initial question asked for a solution onkeyup.
So no hard feelings.
>> Perhaps the comma confused you  <<

The problem was a lot more than a comma.  Even removing the "phrase" your commas were meant to mark the meaning is still the same as I read it at first.  The issue was the sentence itself, just like the "sentence" after the phrase I quoted above.  I don't mind you changing it or saying it wasn't clear but don't blame me for reading it wrong or for a mistake.

>> it uses the onkeydown event <<

No!  It uses the keypress event.  For a keyup event to work it would need to "erase" the value if the validation failed; the keyup is after the key's value is added.  Truly blocking the character from being entered needs an event before keyup, etc.  That makes the script much more complex and likely to fail.  If there is a reason the keypress event won't work then please elaborate.

>> So I will delete the question. ... I have no time to go into details. <<

You will need to be specific on the solution you did use if you want a refund and this deleted/PAQ'd.  The code above is a working solution for what you asked.  I'm glad you got a solution but, with how this has gone, I'm not sure it didn't involve my post.

bol
Avatar of simi

ASKER

I have no time to argue with you.
Your script does not work with IE version 6 if (but worked with Mozilla based brws.),/.
IE has the pop-up blocked, even if you do not pop up something, for some reason, the top bar display a message and nothing happens. I will not go with further explination on that.
I am not answering questions here, I asked one.
But still, here is the working code I was looking for
function upper(textField) {
      textField.value = textField.value.toUpperCase();
}

Against your oppinionn, is even more simple then yours.

I will not continue this argument with you.

All the best.
I never wanted to argue at all.  Your first post seems to have initiated that but, even after that, arguing hasn't been my intent.

I am confused by your comment though.  What popup?  How did that ever become part of this?  There was an alert but the last post removed it.  Any popup, even if it is blocked, is part of something else, not my code.  The page did work in IE 6 for me.

>> I am not answering questions here, I asked one. <<

You have to answer questions if you expect to get help.  To get an answer you will almost always have to interact with the experts by answering and responding to them.

The "working code" you posted doesn't seem to work for the second or third items you asked for.  Also that code is also used for the whole iinput value.  On its own it won't get a character or change it like in an onkeyup event.

I notice now that I did read the first part backwards.  Your use of toUpperCase() in your "solution" was what I should've suggested in my first post instead of toLowerCase().  Sorry about misreading that.  The correct figure numbers to use in my chkKeyLower function would be as follows if you want uppercase characters instead.

function chkKeyLower(e) {
      var k = (e.which)? e.which : e.keyCode;
      if (k < 65 || k > 90) {
            return false;
      }
      return true;
}

Of course the function name could be changed too if you change the html event as well.

This post is mainly for the Moderator if you are really not interested anymore.  I'll leave it to them to decide what is best for this.  If you do still need help and want to work with someone that can help then just let me know.

bol
Avatar of simi

ASKER

This is for the moderators.
Please delete this question.

And, if possible, advise how to deal with "experts" that won't let go.

:-)
Vee_Mod - The CS question you linked is a different member than the Asker and seems to be a different question.  Just wanted to give you a heads up in case your notice was posted in the wrong question.  I do like the recommendation though and am not objecting to it.

bol
Accepted by proxy.
ee_ai_construct
community support moderator
replacement part #xm34
Avatar of simi

ASKER

I have never accepted any solution.
On the opposite, I asked question to be deleted.
Not sure what exactly happened. I find this process very unclear.