Aamec Aamec Aamec Aamec - 6 days ago 5
ASP.NET (C#) Question

Textbox validation in asp.net using javascript

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?

Answer

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.

Comments