Select the Textbox value

Asked

Viewed 49 times

0

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

1 answer

-2


I believe that the simplest solution is to do this via javascript. Take a look at this link: https://stackoverflow.com/questions/4067469/selecting-all-text-in-html-text-input-when-clicked

Here’s an example of what it would look like: https://jsfiddle.net/4rwLw5yw/

$.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>
<input type="text" id="MyTextBox" value="teste" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.