Manivel
Manivel

Reputation: 39

How to Select particular text in textbox while textbox receives focus

I am developing one ASP.NET application with VB code.I have one textbox to hold the amount, which contains default value as "0.00". Now the problem is while the textbox gets focus it selects all the text. But i want to select only 0 that is before precision and I do not want to select after precision.

Hope someone will help me. Thanks in advance

Upvotes: 2

Views: 1552

Answers (3)

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

assume you have TextInput you want to select its first character, whenever its selected

<input id="MyText" type="text" value="text" onclick="MyText_click()" />

and the script is like this:

<script>

    function MyText_click() {

        var input = document.getElementById("MyText");
        createSelection(input, 0, 1); // first character
    };

    function createSelection(field, start, end) {
        if (field.createTextRange) {
            var selRange = field.createTextRange();
            selRange.collapse(true);
            selRange.moveStart('character', start);
            selRange.moveEnd('character', end);
            selRange.select();
            field.focus();
        } else if (field.setSelectionRange) {
            field.focus();
            field.setSelectionRange(start, end);
        } else if (typeof field.selectionStart != 'undefined') {
            field.selectionStart = start;
            field.selectionEnd = end;
            field.focus();
        }
    }

</script>

Upvotes: 2

Dilip Suvagiya
Dilip Suvagiya

Reputation: 394

Create event for text focus

Now we have two properties

   txt.SelectionStart = intValue

and

  txt.selectionLength = length;

Use this property to resolve your stuff

Upvotes: 0

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

you can do that programmatically using javascript, see this: Programmatically selecting partial text in an input field

Upvotes: 0

Related Questions