Changing a textbox to "non-required"

15 views
Skip to first unread message

crocboy25

unread,
Feb 2, 2011, 2:53:21 PM2/2/11
to Community for ASP.NET MVC
Group,

I have a textbox set to required in model I am using as shown below:

<DisplayName("Last Name"), Required()> _
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property


When a user checks a checkbox on the page, I want to make this
LastName textbox option(nonrequired) using JQuery. Is there a way
anyone knows how to do this? Here is some sample JQuery I am doing
during an event when the box is checked....

$('input[name="People_1__LastName"],
label[for="People[1]_LastName"]').hide();
$("#People_1__LastName").hide();
$("#People_1__LastName").val("hello");
$("#People_1__LastName :input").attr('disabled',
true);

Any help with this would be appreciated.

TCW

Rajendra Prasad

unread,
Feb 12, 2011, 8:06:43 AM2/12/11
to c4...@googlegroups.com
Hi,

is the code wat u have specified is working?

or u need a simple jQuery ?

Pardon me if i am unable to understand wat u have asked

--
You received this message because you are subscribed to the "Community for ASP.NET MVC" group.

To post to this group, send email to c4...@googlegroups.com

To unsubscribe from this group, send email to
c4mvc+un...@googlegroups.com
For more options, visit this group at
http://CommunityForMvc.Net

Rajendra Prasad

unread,
Feb 12, 2011, 8:40:44 AM2/12/11
to c4...@googlegroups.com

 <p id="phide">
                                                    <label for="Textbox_Lastname">
                                                        GuardianAddress:</label>
                                                    <%= Html.TextBox("Textbox_Lastname") %>
                                                    <%= Html.ValidationMessage("Textbox_Lastname", "*") %>
                                                </p>
                                                <p>
                                                    <label for="status">
                                                        Status:</label>
                                                    <%= Html.CheckBox("status") %>
                                                    <%= Html.ValidationMessage("status", "*") %>
                                                </p>



<script language="javascript" type="text/javascript">

 $('#status').click(function() {
            var thisCheck = $(this).val();
            alert(thisCheck);
            if (thisCheck) {
                alert("checked");
                 $('#phide').hide();
            }
        });
</script>

John Petersen

unread,
Feb 12, 2011, 9:41:33 AM2/12/11
to c4...@googlegroups.com
A few things...

To get the jquery call to happen on page load, you need to wrap with the document ready call:

$(document).ready(function() {

 $('#status').click(function() {
            var thisCheck = $(this).val();
            alert(thisCheck);
            if (thisCheck) {
                alert("checked");
                 $('#phide').hide();
            }
        });
});

Now, the function is bound to the click event..

Another thing...how about re-displaying the textbox (and simplyfying the code a bit too!!). I think this will work:

$(document).ready(function() {
   $('#phide').hide();
   $('#status').click(function() {
      if !($(this).val());
         $('#phide').show();
    }
});

Another way to handle these sort of requirements is through css styling. Instead of explicitly invoking show and hide (which behind the scenes is just doing the same thing with styles) - create your own styles and use jQuery to apply the styles accordingly.

John

Stephen Russell

unread,
Feb 12, 2011, 7:18:25 PM2/12/11
to c4...@googlegroups.com
Hi

On 3/02/2011 6:53 AM, crocboy25 wrote:
> When a user checks a checkbox on the page, I want to make this
> LastName textbox option(nonrequired) using JQuery.

No.

The answers you are getting are simple making the textbox appear and
disappear in the browser. This has no impact on the data validation that
will occur when the data are submitted back to the model.

Try again.

Steve.

Reply all
Reply to author
Forward
0 new messages