Alon M
Alon M

Reputation: 1683

Get hebrew chars from textbox in asp.net

So,

I've got a textbox in my website, which is written in c# asp.net 4.

<td>
    <asp:TextBox ID="UserName" runat="server" CssClass="textEntry" Width="265px" 
     placeholder="שם משתמש" ontextchanged="UserName_TextChanged"></asp:TextBox>
</td>

The problem is, that when someone trying to put Hebrew input, it lets him do it.

How can I know that the input he is trying to write is Hebrew and don't let him? Just like show him a warning in javascript, or just don't let him.

Anyone can give me a hand?

thanks!

Upvotes: 1

Views: 738

Answers (3)

Shai Cohen
Shai Cohen

Reputation: 6249

I believe this is what you are looking for:

function UserName_TextChanged(which) {
    if (/[^a-zA-Z]/gi.test(which.value)) {
        alert ("Only alpha characters are valid in this field"); // no spaces, full stops or anything but A-Z
        which.value = "";
        which.focus();
        return false;
    }
}

I don't have Hebrew on my machine, but I believe it will stop those characters as well

You call this method like so:

ontextchanged="UserName_TextChanged(this)"

Explanation of code:

function UserName_TextChanged(which) {

The "which" variable is the control you are validating. Notice when you call the function, you pass "this", which loosely translates to "this object"

if (/[^a-zA-Z]/gi.test(which.value)) {

This tests a regex pattern against the value of the control you passed in

alert ("Only alpha characters are valid in this field");

If the pattern matches (meaning there are characters other than a-z), you are alerting the user that they entered invalid characters

which.value = "";

Here you are erasing the control's text. You might not want to do this, depending on your needs.

which.focus();

Here you are putting the cursor back into the control you are validating.

return false;

This is used in case you are calling this validation before submitting a form. By returning false, you can cancel the submission of the form.

Upvotes: 1

Sam Leach
Sam Leach

Reputation: 12956

There are many Stackoverflow answers on this. Use search. "Hebrew Input Validation".

Upvotes: 0

Amit
Amit

Reputation: 22076

I think you need this type of solution TextBox only write String Character

How to allow only numbers (or letters) in a text box with JavaScript. For example

 function ynOnly(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
            ((evt.which) ? evt.which : 0));
        if (charCode > 31 && charCode != 78 && charCode != 89 &&
            charCode != 110 && charCode != 121) {
            alert("Enter "Y" or "N" only.");
            return false;
        }
        return true;
   }

In this case, you could add some more protection against incorrect entries by limiting the text box to a single character:

 Signature Present: <input type="text" name="signature" size="2" maxlength="1"
  onkeypress="return ynOnly(event)" /> (Y/N)

You can alter this code according to your requirements.

Upvotes: 3

Related Questions