0

Possible Duplicate:
disable textbox using jquery?

How to prevent changing a textbox value using jquery in document.getElementsByName(). There is an option like this.

document.getElementById("Quantity").defaultValue;

Is there any option to stop my textbox's value from changing in document.getElementsByName(). After my alert, I want to make the value of textbox stay the same when the condition is not matched. My code:

 $(function () {
            $('input[name=Quantity]').blur(allownumbers);
        });
        function allownumbers() {
            var elements = document.getElementsByName('Quantity');
            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('There is no enough inventory for this product to fulfill your order');
                    return false;
                }
            }
            return true;
        }
  <input id="Quantity" type="text" name="Quantity"/>

Any suggestions.

EDIT : I tried using this code.

$("#textbox1").removeAttr("disabled"); 

If I enter 200. The alert appears and the textbox becomes disabled and so I cant even re-enter the correct value in textbox. How to bring back the defaultvalue of my textbox. ?

Community
  • 1
  • 1
kk1076
  • 1,740
  • 13
  • 45
  • 76
  • 1
    http://stackoverflow.com/a/1649023/1538708 – adamb Nov 01 '12 at 15:59
  • try `$(yourtextbox).attr('disabled', true)` or `document.getElementById('<%=textbox %>').disabled = true` – huMpty duMpty Nov 01 '12 at 16:04
  • *"Is there any option to stop my textbox's value from changing in `document.getElementsByName()`."* `getElementsByName` **doesn't** change the value of the text box (or anything else). Neither does your code. What is it exactly you want to achieve? (If English is an issue, perhaps colleagues could help?) – T.J. Crowder Nov 01 '12 at 16:05

2 Answers2

1

Like adamb posted, you can just disable the input. The alternative is to set an attribute on the input (maybe defaultval='the default') and replace the value of the input with the defaultval attribute.

In your example, it may be best for your customers to have the input actually be set to the limit. For instance, you have a condition of 100 being the max, so if someone puts in a bigger number, say 200, it will change the box to the max possible, 100.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • 1
    There's no need for a separate "default" value. The [`defaultValue` property](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26091157) the OP highlighted in the question **is** the default value (the value of the `value` attribute as of when the element was created). – T.J. Crowder Nov 01 '12 at 16:10
  • Oh, it's a default attribute? Somehow, I didn't know this... learn something new every day. – Randy Hall Nov 01 '12 at 16:30
  • @ Randy: Yeah, it's not well-known, but it's quite well-supported. :-) – T.J. Crowder Nov 01 '12 at 16:36
1

you would store the original value in a variable or hidden field then replace the value if the update failed. If the range is known when the page loads, you can use a range validation.

An alert is generally too obtrusive for a validation on blur.

James Fleming
  • 2,589
  • 2
  • 25
  • 41