0

I have two textboxes in which I want to limit the user, so he can only use Y or N in that textbox. How can i achieve

.yes_or_no{
  font-size:xx-large;
  font-weight:900;
  color:#000;
}
<input id="t1" class="yes_or_no" type="text" class="typing1"  name="txt1"   oninput="this.value = this.value.toUpperCase(),  this.value.replace(/[^0-9]/, '')"  maxlength="1" />
<input id="t2"class="yes_or_no" type="text" class="typing2"  name="txt2"   oninput="this.value = this.value.toUpperCase(),  this.value.replace(/[^0-9]/, '')"  maxlength="1" />

4
  • You can use a combination of the size and maxlength attribute example. Commented Aug 17, 2018 at 10:54
  • add minlength and maxlength to your input Commented Aug 17, 2018 at 10:54
  • 1
    i used maxlenth to restrict the limit to 1 but i want to the user to type either Y or N and no other input Commented Aug 17, 2018 at 10:59
  • So this is primarily about which letters should be allowed, and maxlength is just part of the proposed solution? If you offer only two choices, why not use radio buttons? Commented Aug 17, 2018 at 12:08

2 Answers 2

4

You're almost there! Instead of replacing the non-numeric characters with blank, you can replace anything not Y or N with blank.

See the following demo:

<input type="text" class="typing1" name="txt1" id="t1" oninput="this.value = this.value.toUpperCase().replace(/[^YN]/, '')" maxlength="1" />
<input type="text" class="typing2" name="txt2" id="t2" oninput="this.value = this.value.toUpperCase().replace(/[^YN]/, '')" maxlength="1" />

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

Comments

1

you have to define a pattern to only accept Y or N. - documentation

See if this is what you are looking for.

while not putting Y or N does not submit form.

<form action="/action_page.php">
  Question?: <input type="text" name="question" pattern="^(?:Y\b|N\b)" maxlength="1" title="Introduce Y or N">
  <input type="submit">
</form>

1 Comment

actually want to enter only y or n and no other input ...thanks for reply

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.