简体   繁体   中英

validate a text field with only 5 value

 <label class="user-reg-label fl">Percentage:<span class="required">*</span></label>
     <input class="user-reg-input fl" type="text" name="Percentage[]" id="Percentage1" value="" maxlength="5" />

this is my html code. i need to validate this input field with javascript to enter only 5 values like 99.99

This is formatting, not validating, but it seems to be what the OP is really looking for ( I think ).

var input = document.getElementById("Percentage1"),
    onlyNumbers = input.value.replace(/\D/g,""), // Remove all nondigits
    cleanNumbers = onlyNumbers.substr( 0, 4 ); // Limit to four digits

// Add decimal if more than two digits in length
if( cleanNumbers.length > 2 )
{
    cleanNumbers = cleanNumbers.substr( 0, 2 ) + "." + cleanNumbers.substr( 2 );
}

input.value = cleanNumbers;

Using Jquery something like the below works.

Textbox

 <input class="user-reg-input fl" type="text" name="Percentage[]" id="Percentage1" value=""  onpaste="return false"
        onkeypress="if(event.keyCode<48 || event.keyCode>57)event.returnValue=false;" maxlength="6" />

Jquery

$(document).ready(function ()
  {
     $('#Percentage1').keyup(function (event)
    {
        var currentValue = $(this).val();
        var length = currentValue.length;

        if (length == 2)
        {
            $(this).val(currentValue + ".");
        }
        else if (length == 5)
        {
            $(this).val(currentValue + "%");
        }
    });
});

This works for your basic requirements however there are a few things that need to be improved. If a user tries to delete multiple numbers it doesn't work or if they try to use the backspace key

You can use keyup or keypress function for this

<input onkeyup="myFunction()" class="user-reg-input fl" type="text" name="Percentage[]" id="Percentage1" value="" maxlength="6" />

function myFunction()
{
var input = document.getElementById("Percentage1");

if( input.value.length > 6 )
{
alert( "too many characters" );
return false;
}
}

UPDATED:

<input type="text" id='text_field' onkeyup="check()"/>
    <script>
        function check()
        {
            var len=document.getElementById('text_field').value.length;
            if (len==2)
                document.getElementById('text_field').value=document.getElementById('text_field').value+".";
            if (len==5)
                document.getElementById('text_field').value=document.getElementById('text_field').value+"%";
        }
</script>

Okay then there is a text field where only if you put "99.99%" type pattern it will validate

    <script type="text/javascript">
function fnc(){
pattern=/\d+[.]+\d+[%]/g;
text=document.getElementById("atext").value;
x=pattern.test(text);
alert(x);
}
</script>
<input type="text" maxlength="6" id="atext">
<input type="button" onClick="fnc()" value="click me">

Try this one,

With Script portion

<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#test').blur(function() {
        if ($(this).val().match('(^100(\.0{1,2})?$)|(^([1-9]([0-9])?|0)(\.[0-9]{1,2})?$)')) {
                alert("Percentage Matched");//Any thing which you want

            }
            else
                alert("Not Valid Input");
        });
    });
</script>

And Code for Textbox

<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="test" runat="server"></asp:TextBox>
</div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM