0

I have the 7 textboxes, only 2 digit numeric values are allowed, must allow 01-49 number only, and the same number must not repeatable.

Ex:

01 20 03 04 05 06 49

My code,

<script type="text/javascript">
    function integersOnly(obj) {
        obj.value = obj.value.replace(/[^0-9-.]/g, '');
    }
</script>

only satisfies numeric input, but how to check the other criteria?

1 Answer 1

1

I have implemented coding based on your condition.

HTML CODE:

<input type="text" class="txt" />
<input type="text" class="txt" />
<input type="text" class="txt" />
<input type="text" class="txt" />
<input type="text" class="txt" />
<input type="text" class="txt" />
<input type="text" class="txt" />

JAVA SCRIPT CODE

$(function() {
    $(".txt").on("keyup", function(event) {
        // don't handle backspace, delete, pgup/pgdn, home/end, or arrow keys:
        if (event.keyCode === 8 || event.keyCode === 46 || event.keyCode >= 33 && event.keyCode <= 40) return false;

        var currentEl = $(this);
        var value = $(currentEl).val();

        // remove letters...
        value = value.replace(/[^0-9-]/g, "");

        // handle numbers greater than 49... :
        if (parseInt(value) > 49 || parseInt(value) === 49) {
            value = "49";
        }

        $(currentEl).val(value);
    });

    $('input.txt').change(function() {
        var textValues = new Array();
        $("input.txt").each(function() {

            if ($(this).val() != "") {
                doesExisit = ($.inArray($(this).val(), textValues) == -1) ? false : true;

                if (!doesExisit) {
                    textValues.push($(this).val())
                } else {
                    alert('Same value has been already exists.');
                    return false;
                }
            }
        });
    });  
});

Can please go through this JSFiddle link: http://jsfiddle.net/997g3fug/18/

Please Share your thoughts. Hope this might help you out.

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

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.