0
<label>Year</label>
<asp:Textbox id="year" runat="server" placeholder="year">

To try enter this textbox to allow this text box only allow till 2018.thanks in advance.

3
  • Just to clarify you want users to be able to enter a year greater or equal to 2018 in the Textbox or is it something different? (i.e. 2018, 2019, 2020, etc) Commented Jul 26, 2018 at 12:21
  • you can use max attribute of html Commented Jul 26, 2018 at 12:46
  • 2
    Why not just use a select with year options descending from the current? Much simpler to implement and zero input validation required Commented Jul 26, 2018 at 12:49

2 Answers 2

1

If you want to have it in simple html we have attribute max

<input type="number" id="year" max="2018" message="Year should be less that 2019"/>

Or in jQuery

    $(document).ready(function(){
     $("#year").on('input',function(){
      if(parseInt($(this).val()) > 2018){
       alert('Year must be less than 2019');
      }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Year</label>
   <input type="number" id="year" placeholder="year">

Sign up to request clarification or add additional context in comments.

7 Comments

What about if someone pastes a value in to the field?
@RoryMcCrossan. Are you saying pasting is not working?
@mani. If is was useful, please upvote and mark as answer. If you need anything else, please mention, so that I can modify answer as per you requirement
Yes. If I copy the text 2019 and then right click the field and choose 'paste' the value will be entered without validation. In other words, always use the input event, not keyup.
@RoryMcCrossan. Very thanks for your suggestion. I have modified my answer, Can you check?
|
0

Please try below code and don't forget to check the datatype too:-

<script>
/* code for jQuery */
$('#id').click(function(){\
    if($('#year').val() > 2018){
       //html message or alert box whatever you want.
    }
});

/*code for core javascript */
function checkYear(){
     var year = document.getElementById('year').value;
     if(year > 2018){
        //html message or alert box whatever you want.
     }
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.