Omari Omosa February 2016

How to restrict a text input only to accept letter M and F using HTML 5(preferably) or javascript(no jquery)

Here is a textbox, and i do not want to use dropdown for some reasons

<input type="text" required="required" name="gender" value="M" 
              size="32" />

Answers


gurvinder372 February 2016

use pattern attribute, check this demo

 <input type="text" required="required" name="gender" value="M" pattern="M|F" size="32" title="only letter M or F is allowed" />


abhay vyas February 2016

hope this will usefull for you

<script type="text/javascript">
    function f(v)
    {
        if(v=='F' || v=='M' || v=='f' || v=='m')
        {
            document.getElementById("t").value=v;
        }
        else
        {
            document.getElementById("t").value="";   
        }
    }
</script>
<input type="text" onkeyup="f(this.value)" id="t" required="required" name="gender" value="M" size="32" />

Post Status

Asked in February 2016
Viewed 1,767 times
Voted 6
Answered 2 times

Search




Leave an answer