Select the TextBox value

0

Is it possible, when entering a page, to leave the value of a textbox selected, and if the value is wrong, instead of deleting, is it just to enter another value that the previous value is automatically deleted?

    
asked by anonymous 25.08.2015 / 19:52

1 answer

-2

I think the simplest solution is to do this via javascript. Take a look at this link: link

Here is an example of what it would look like: link

$.fn.selectRange = function(start, end) {
    var e = document.getElementById($(this).attr('id')); // I don't know why... but $(this) don't want to work today :-/
    if (!e) return;
    else if (e.setSelectionRange) { e.focus(); e.setSelectionRange(start, end); } /* WebKit */ 
    else if (e.createTextRange) { var range = e.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', start); range.select(); } /* IE */
    else if (e.selectionStart) { e.selectionStart = start; e.selectionEnd = end; }
};

$(function (){
    //aqui a verificação se o seu campo esta valido
    var myTextBoxIsValid = false;
    
    if (!myTextBoxIsValid)
    	$('#MyTextBox').selectRange(0, 10);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" id="MyTextBox" value="teste" />
    
25.08.2015 / 20:23