Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi In my current project in MVC3, I am building a page having bunch of textbox, the requirement is to enable the validation on the controls only if the user enters value in any one of the field. If there is a value in one text box then the user should enter value in every text box. In MVC3, specifying "[Required]" attribute to my model properties enforce the validation to all controls and at all time, defeating my requirement. So i would need to check if any text box has value entered in client side, post then the Required validation for these fields should be enabled..

Please some one help me with this.. The week long search for this problem makes my hair falling out of my scalp :(
Posted

1 solution

You can do such thing

JavaScript
$("#btnSaveData").click(function (event) {
    if ($('#name').val().length > 0)
    {
        if ($('address').val().length == 0)
        {
            alert('Address is required.');
        }
    }
});
 
Share this answer
 
Comments
Member 10856181 2-Jun-14 2:13am    
Hey Vicky.. Thanks for your solution. I can't have the javascript alerts for the validation error message, it should be exactly in the same way how it would display for controls with the "[Required]" attribute enabled.
It should display in the same way with the inline color in the textbox.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900