Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  83] [ 6]  / answers: 1 / hits: 41753  / 14 Years ago, tue, february 15, 2011, 12:00:00

I have text boxes <input type='text'> that only allow numeric characters and wont let the user enter a dot (.) more than once. Problem is, if the text in the text box is selected, the user intends to overwrite the contents with a dot, hence making it allowed! The question is, how can you tell in javascript whether the text in that text box is selected or not.



Thanks


More From » jquery

 Answers
15

The following will tell you whether or not all of the text is selected within a text input in all major browsers.



Example: http://www.jsfiddle.net/9Q23E/



Code:



function isTextSelected(input) {
if (typeof input.selectionStart == number) {
return input.selectionStart == 0 && input.selectionEnd == input.value.length;
} else if (typeof document.selection != undefined) {
input.focus();
return document.selection.createRange().text == input.value;
}
}

[#93729] Sunday, February 13, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;