Mostly dis can be possibly duplicate. But I dont know why I get this error.
My Script code .
$(document).ready(function () {
$(function () {
$('input[name=Quantity]').blur(allownumbers);
});
function allownumbers() {
alert('ad');
var elements = document.getElementsByName('Quantity');
alert(elements);
for (var i = 0; i < elements.length; i++) {
if (elements[i].value == '' || elements[i].value < 1) {
alert('Please enter a valid value');
return false;
}
else if (elements[i].value > 100) {
alert('Please enter a value less than 100');
return false;
}
}
return true;
}
});
My page code :
<input id="Quantity" type="text" class="TxtBox" name="Quantity" value="@item.Quantity" onblur="return allownumbers()" maxlength="3"/>
And the script references which I have was
<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript">
What’s wrong with my code. Any suggestions ..
EDIT :
The function is not even called when I dont call it in the textbox.
You have duplicated your script references. You need only include each script once (preferrably the minified version unless it is a script you are actively debugging and writing). Secondly your initialization routine needs some help.
Now then, the allownumbers does not have to be outside of the original block, but for me it helps keep my scope clean. Secondly, $(document).ready ensures that your document is ready for use, so $(function () {}) is not necessary for JQuery call.
So eliminate duplicate script references, and you should be good.