0

i have a input textbox in html and i want to allow the user just 'Y' or 'N' and if he tries to enter any other character then it should show an alert dialog box. so can anyone help me into dis??

6
  • You may want to take a look at jQuery's .keypress() function for validating this with Javascript. When submitting the form, you will still want to validate the output on your server side as well. Commented Mar 30, 2013 at 6:46
  • 4
    why not use a dropdown or radio button? Commented Mar 30, 2013 at 6:47
  • use onkeypress="javascript_function_to_validate()" Commented Mar 30, 2013 at 6:47
  • element.onkeydown = function() { if (!this.value.trim().match(/(Y|N)/)) alert('nope'); return false;} Commented Mar 30, 2013 at 6:59
  • @TamilSelvan - You shouldn't suggest using an inline event handler. It's a bad practice. Commented Mar 30, 2013 at 6:59

5 Answers 5

2

jQuery version

$('input').keypress( function( e ){ 

    $(this).val('');

    var code = e.which || e.keyCode ; 

    if ( !( code == 89 || code == 121 || 
            code == 78 || code == 110 ) ){

         alert('you entered wrong key');

         e.preventDefault();                   
    }

});

check it on jsfiddle http://jsfiddle.net/TTgKF/

inline javascript version

<input id="keypress" onkeypress="return allowYN( this, event );" />

and allowYN define as

function allowYN( el, event) {

    event = event || window.event;
    var charCode = (event.which) ? event.which : event.keyCode;

    el.value = '';

    isYN = (charCode == 89  || charCode == 121  || 
            charCode == 78  || charCode ==110 );

    if ( !isYN ) {

            alert('you entered wrong key');
    }

    return isYN;
}

You can add exception for Delete key (46), Backspace key (8) ..

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

1 Comment

A link to the keycode table would be better than providing him codes one key at a time...
0

Assuming you're able to get the HTML element by ID, this would check the #myInput element and only accept Y or N.

if ((document.getElementById('myInput').value == 'Y') ||
    (document.getElementById('myInput').value == 'N')) {
    // Do stuff if it's correct.
} else {
    alert("You're doing it wrong!");
}

Comments

0

As previously noted, the best option is to use radio buttons:

<input type="radio" name="yn" value="Y">Y<br>
<input type="radio" name="yn" value="N">N<br>

Then either set one as selected or check on submit that one is selected. Alternatively, you can use scripted input elements, but it is not sensible:

<script>
function validateYN(element) {
  var errNode = document.getElementById(element.id + '_err');
  if(/^[YN]$/.test(element.value)) {
    errNode.innerHTML = '';
  } else {
    errNode.innerHTML = "You must enter Y or N";
  }
}
</script>

Please enter Y or N: <input name="foo" id="foo" onchange="validateYN(this);"
                      onblur="validateYN(this);">
                     <span id="foo_err"></span>

Comments

0

you could also try this

 $("input").live('keypress', function(e) {
     if!( code == 89 || code == 121 || 
            code == 78 || code == 110 ) )  { 
            e.preventDefault();
            }
        else //your code

Comments

0
<html> 
<head>
    <script type="text/javascript">
        function keyPressed(evt)
        {
            var theEvent = evt || window.event;
            var k = theEvent.keyCode || theEvent.which;
            if(k == 89 || k == 78 || k == 8 || k == 46)
                return true;
            else{
                alert("Your Message");
                evt.preventDefault();
                return false;
            }
        }
    </script>
</head>
<body>
    <input type="text" onkeydown="keyPressed(event)" />
</body>
</html>

1 Comment

@RobG - Yeah... I guess I knew that was a sketchy, subjective link. Though I thought it was pretty much concensus that inline listeners were bad practice.

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.