2

How to restrict a textbox to accept 10 characters only.If we enter 11th character, it should give a message saying 'you are allowed to enter 10 characters only'.

pk2218
  • 111
  • 2
  • 9
  • Use max length for text box –  Mar 28 '14 at 10:13
  • @ Let's Code ...I want it not to accept numeric data.I want it to accept alphabets only. – pk2218 Mar 28 '14 at 10:16
  • you will want both front and backend validation of any information you're gathering. if someone forces longer length code to be submitted in your form you you can check it's length using `strlen($inputvalue)` (http://au2.php.net/strlen) and then either trim it to length using `substr($inputvalue, 0, 10)` (http://au1.php.net/substr) or return an error message without accepting the submitted form – haxxxton Mar 28 '14 at 10:18
  • Doing it 'using php' as you asked is not possible or in other words senseless, as that is not php's purpose. – Robin Mar 28 '14 at 10:19
  • @codezombie depends if by 'restrict a textbox' he means front end or code that eventually makes it into storage – haxxxton Mar 28 '14 at 10:20
  • @haxxxton - you're right, but the 'give a message' part made me thinking of a frontend check ;) – Robin Mar 28 '14 at 10:22
  • @codezombie agreed :) – haxxxton Mar 28 '14 at 10:23
  • @codezombie..I want to perform a front end check.I want the textbox to accept 10 alphabets only. – pk2218 Mar 28 '14 at 10:26
  • @pk2218 have a look at the answer to this question http://stackoverflow.com/a/6256040/648350 but remove the 0-9 – haxxxton Mar 28 '14 at 10:27

3 Answers3

1

Try with this function:

 function check_content(){
        var text = document.getElementById("your_textbox_id").value;
        if(text.length > 10){
            alert('Length should not be greater than 10');
            return false;
        } else { 
            return true;
        }
    }
Arkalex
  • 101
  • 6
0
Try this code:

<input type="text" id="Textbox" name="Textbox" maxlength="10" />
Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
0

Code for textbox.

<input name="mytext" type="text" value='' id="mytext" onkeyup="TextLimit()">

and add a javascript function on keyup event of textbox.

<script>
function TextLimit(){
    var text = document.getElementById('mytext');
   if (text.value.length >= 10){
        alert('you are allowed to enter 10 characters only');

    }
}  
</script>
Jenz
  • 8,280
  • 7
  • 44
  • 77