2

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

Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
Manivel
  • 39
  • 1
  • 2
  • 10
  • There is no such functionality and no control in ASP.NET. You could create your own `UserControl` which encapsulates the logic. You could for example use two `TextBoxes` side by side or you need to fiddle around with javascript . – Tim Schmelter Feb 06 '14 at 14:01
  • Thanks for your comment. Please suggest me some other method to do it except javascript. – Manivel Feb 07 '14 at 07:58

3 Answers3

2

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>
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
  • i just copy the `function` from here [Programmatically selecting partial text in an input field] (http://stackoverflow.com/questions/646611/programmatically-selecting-partial-text-in-an-input-field) which already had commented, and call it from `onclick` handler – Amir Sherafatian Feb 07 '14 at 08:15
  • Thanks alot it works. Instead onclick can I use onfocus?? Because I want to select while tab event. – Manivel Feb 07 '14 at 08:47
  • yes, you can call javascript functions from everywhere you be able to write javascript, if posts are useful to you, vote them, and if they are your answer check them as answer, for other who see your posts – Amir Sherafatian Feb 07 '14 at 08:52
  • hi I have another one doubt. For example if I have the following values 5000.00 , 250000.00, 100.00 now i want to select the values before precision. how to calculate the length of digits before precision? – Manivel Feb 12 '14 at 07:30
  • you can do this, by `regular expression` and `indexOf`... currently i can't get exact answer, sorry – Amir Sherafatian Feb 12 '14 at 08:24
  • i am using the following condition in for loop `if(message.value(i).toString == String.fromCharCode(46))` to find the .(precision) in text value. Is it correct method? – Manivel Feb 12 '14 at 08:41
  • if you are sure that your value is always from this format (e.g. 234200.00) you can use `indexOf` method which get you the index of character you want. in your case, you can call selection method like this `createSelection(INPUT_ELEMENT, 0, INPUT_ELEMENT.value.indexOf("."))`, but if your value may be invalid format like `200.00.00` (user can enter any value without any validation) you need yo validate your value using `regular expression` before selecting the value – Amir Sherafatian Feb 12 '14 at 16:51
0

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

Community
  • 1
  • 1
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
  • Thanks for your reply. This works in window.onload, I donot want to perform in this way. I want to perform this selection in tab event or mouse click event. – Manivel Feb 07 '14 at 08:00
  • !!! you can copy and paste the `function` does select, and call it every where you want, attend my next answer – Amir Sherafatian Feb 07 '14 at 08:08
0

Create event for text focus

Now we have two properties

   txt.SelectionStart = intValue

and

  txt.selectionLength = length;

Use this property to resolve your stuff

Dilip Suvagiya
  • 396
  • 1
  • 4
  • 14
  • Thanks for your reply. SelectStart and SelectionLength is only available in Windows Forms application. My textbox is web control. So these properties are not shown in my intelisence. Is there any other way to solve this problem? – Manivel Feb 07 '14 at 07:55