This is a migrated thread and some comments may be shown as answers.

OnKeyPress and caret position

5 Answers 266 Views
Input
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 17 Jul 2010, 01:37 AM
Hello,

Is there a feasible way of determining what the new value will be of an input control inside of a OnKeyPress client-side handler?

- I've seen a lot of postings here showing sender.get_value() + args.get_keyCharacter() - but of course that assumes that the user is typing straight through and the cursor is at the end of the control. Not always the case! ;)
- The reason I'm looking to do this is to implement regex validation in real time (keydown or OnKeyPress), before the control loses focus. OnValueChanged and OnValueChanging occur on blur. (ASP.NET validation on post-back is clumsy and not appropriate for the circumstances we need this).

Ideally, though, Telerik could release a RegEx version of RadTextBox!

Thanks,

- Mike

5 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 21 Jul 2010, 01:49 PM
Hi Mike,

You can attach an onkeyup javascript event to the RadTextBox in the PageLoad event handler, by using the following code:
RadTextBox1.Attributes.Add("onkeyup", "OnKeyUp(this, event);");

Then, it is easy to get the full value (including the last-typed character) of the text box, by using the get_textBoxValue() client-side method of the control:

function OnKeyUp(sender, eventArgs) {
            var textBox = $find("<%= RadTextBox1.ClientID %>");
            var textInput= textBox.get_textBoxValue();
        }


Greetings,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mike
Top achievements
Rank 1
answered on 21 Jul 2010, 05:46 PM
Hello,

Thanks for your response. The problem with examining the value on KeyUp is that I want to do real-time validation of the text box value - as you do with your other textbox-based controls. If I wait until KeyUp, the value is already there in the text box, visible to the user.

- Mike
0
Tsvetina
Telerik team
answered on 22 Jul 2010, 10:47 AM
Hi Mike,

In this case you can wire OnKeyPress client-side event of the control, take the text box value, split it at the caret position and insert the new key character there. In this way you will get the new value of the textbox before the new character appears in it.

function OnKeyPress(sender, eventArgs) {
            var oldText = sender._textBoxElement.value;
            var substr1 = oldText.substr(0, sender.get_caretPosition());
            var substr2 = oldText.substr(sender.get_caretPosition());
            var newText = substr1 + eventArgs.get_keyCharacter() + substr2;
        }

I hope this helps.

All the best,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nicolaas
Top achievements
Rank 1
answered on 23 May 2011, 04:24 PM
This doesnt work if the user selects the the text (currently in the textbox) and then types.

The this code appends it or puts it in front (where the cursor is)
0
Tsvetina
Telerik team
answered on 26 May 2011, 12:28 PM
Hi Nicolaas,

In such scenario you can check in advance whether the text in the textbox is already selected, using an approach similar to:
http://stackoverflow.com/questions/3046623/check-if-an-input-item-text-is-already-selected

All the best,
Tsvetina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Input
Asked by
Mike
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Mike
Top achievements
Rank 1
Nicolaas
Top achievements
Rank 1
Share this question
or