1

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!

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Nope, its not what i am looking for.
So what are you looking for than? The jams' sample shows how to block characters during keyboard input which seem to answer your current question. Please consider editing your question with details on what you are/aren't looking for.
1

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.

2 Comments

i didn't understand how to use this, can you help?
Yes it is javascript. May I ask how much experience with javascript you have?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.