• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1287
  • Last Modified:

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.
0
simi
Asked:
simi
  • 9
  • 6
1 Solution
 
b0lsc0ttIT ManagerCommented:
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

0
 
b0lsc0ttIT ManagerCommented:
I left something out of the 3rd suggestion.

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

bol
0
 
b0lsc0ttIT ManagerCommented:
Oops.  Hit submit too quick, forgot the other thing I left out.

3.  onblur="this.value = this.value.match(/^[A-Z ]+$/i);"
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
b0lsc0ttIT ManagerCommented:
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
0
 
simiAuthor Commented:
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.
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
simiAuthor Commented:
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.
0
 
b0lsc0ttIT ManagerCommented:
>> I clearly stated that I do not want behavior that pops up error messages, <<

I don't know what you mean by "clearly stating" but what you said there directly contradicts what you said in your question body.  You said "then display an error messatge" so I provided an alert.  Removing it is easy but don't blame me for it being there!

I did have a big typo, misspelled the word function, and I made another minor change.  The minor change is probably not needed the typo and using keyup were the big issues.  To do what you want you should use onkeypress.  Sorry about not catching that earlier.

I have a complete, working page below.  I removed the alerts since it seems you don't want them now.  You can add them back if you change your mind again. :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<link href="../style.css" rel="stylesheet" type="text/css" />
<title>Chk entry</title>
<script type="text/javascript">
function chkKeyLower(e) {
      var k = (e.which)? e.which : e.keyCode;
      if (k < 97 || k > 122) {
            return false;
      }
      return true;
}
function chkKeyDigit(e) {
      var k = (e.which)? e.which : e.keyCode;
      if (k < 48 || k > 57) {
            return false;
      }
      return true;
}
function chkKeyLetterSpace(e) {
      var k = (e.which)? e.which : e.keyCode;
      var kName = String.fromCharCode(k);
      if (!kName.match(/^[A-Z ]+$/i)) {
            return false;
      }
      return true;
}
</script>
</head>

<body>
<h3>Check the input</h3>

<br /><br />
<form name="form1" action="" method="post">
Only lower: <input type="text" name="input1" onkeypress="return chkKeyLower(event);" /><br />
Only digit: <input type="text" name="input2" onkeypress="return chkKeyDigit(event);" /><br />
Only alpha or space: <input type="text" name="input3" onkeypress="return chkKeyLetterSpace(event);" /><br />
</form>

</body>
</html>

Let me know if you have a question about this or need more info.  If there is some problem or question you might think about using a little nicer approach, I am not helping here for the abuse and I have better places to spend my limited time if you don't appreciate what I do.

bol
0
 
simiAuthor Commented:
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.
0
 
b0lsc0ttIT ManagerCommented:
>> 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
0
 
simiAuthor Commented:
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.
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
simiAuthor Commented:
This is for the moderators.
Please delete this question.

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

:-)
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
ee_ai_constructCommented:
Accepted by proxy.
ee_ai_construct
community support moderator
replacement part #xm34
0
 
simiAuthor Commented:
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now