MVC 3 Client Side Validation Catches Some ValidationAttributes but not others? - asp.net-mvc-3

I'm confused by this behavior:
I have an out-of-the-box MVC3 app. I haven't really done any customization from the what the scaffolding template gives me.
In web.config, clientsidevalidationenabled and unobtrusivevalidation are both true.
I have a class with one field using the Required annotation, one using StringLength and one using RegularExpression. When I'm editing an object, the textboxes for the properties marked with StringLength & Regex report problems instantly in the UI, but the textbox for the Required doesn't.
If I hit SAVE, then "Model.IsValid" is the controller sess the problem with the missing Required and I get the UI error message next to the text box.
If I view the source of the page, I can see that the markup for the required property does have the dataval-req and other related attributes generated by the Unobtrusive validation.
Is this expected behavior? If it is, what's the reason? If it's not, what might I be doing wrong?
Thanks! :)

As long as the page is not posting back to the server, this should be the correct behavior. The required client-validation will fire only if:
You don't enter data and try to post to the server.
You enter data in the text box and then remove it.
Otherwise the user would be inundated with error messages.

I've gotten to the bottom of this behavior, just by banging on the keyboard some more. It's as expected. In the Create view, the behavior is as #Beavis describes. In the edit view, unobtrusive validation prevents the required property from being validated on tabbing BEFORE the first attempt at hitting SAVE. SAVE then does a UI validation (no postback occurs) and shows the error message next to the property. Once I've hit save that first time, that property responds to tabbing. So now if I make it valid, the message disappears on tab. If I erase the contents of the text box, the message reappears on tab.
Thanks for everyone's help.

Related

Tabbed form and validation.

I'm creating a tabbed form with validation, and there is one thing I don't understand how to do: when I hit the submit button, and there is a validation error in some field, the form is not submitted - but if the field is in an hidden tab (a non focused one), there is no error message displayed either.
Is there am easy way to switch to the tab with errors, or to inhibith focusing on following tabs until all errors in current one had been cleared?
I'm new to all the React, JSX and modern JS development, please do not be too much concise! :)
Thanks!
I reproduced the problem and opened an issue on our repository. I'll let you know how it goes.
In the mean time, one way to mitigate this issue would be to create a custom saga. You'll need some knowledge about redux-saga though.
The idea would be to:
catch failed validations actions from redux-form (SET_SUBMIT_FAILED)
trigger a custom notification with an error message indicating the field and the tab where the error occurred. Unfortunately, you'll have to check the fields names to know for which resource the error occurred.

MVC3 - How to remote validate on checkbox click?

I am using the Remote Validation attribute on my domain model, to call an Action that returns Json. This validation occurs on a checkbox and it only kicks in when the form is submitted. I'd like it to act the same way that text boxes remote validation works. As soon as it looses focus, remote validation kick in. Or perhaps on click.
These are screen shots from a test project. The actual project is too busy, that's why I created this sample one.
I'd love to see some suggestions that do not involve using JQUERY in the view. But if that's the only way then be it.
Thank you.
MODEL:
Both properties have the same Remote validation, because I wanted to test if the loosing of focus would trigger the text box to validate, and indeed, it worked. I assumed that the same would happen for the check box, perhaps on click, or loosing focus, but no. Only when I click create the IsNameDuplicate() is called.
CONTROLLER:
VIEW:

Telerik RadTextbox input behaving like watermark

I've a Telerik RadTextbox in one of my .ascx files in a Sitefinity 5.4 website. When a form containing the RadTextbox is submitted and there is some error thrown by the server, and the user goes back and tries to resubmit the form, there is validation message appearing even if there is input showing from the initial submission. It looks like the input from the first submission is treated as watermark.
Any idea why this is happening?
Is the user going back by using the browser back button? Then the validations will still exist. Try to eliminate the errors by clients side validation first, then server side validations. If the errors still exists, you should clear the validation messages on the load of the control (if it is not a postback of course)

people editor no match text

I am creating one sharepoint webpart and it has one sharepoint people editor control. this is an required field. So i am using required field validator for validating the people editor control. But my problem is that when i am entering some junk data (eg: njhdfhfb), and then hit enter, it shows the validation message of "No exact match was found.". and also my validation message Select an employee or group. In my case i needs only my own validation message. But not the control message. How we can avoid this control validation message. Please help me for this. i had tried many ways.. but its not working. Thanks in advance..
In the above case the validator did not find any users and that is why it showed you the error message. You don't have to use a required field validator use AllowEmpty property and set its value to True.
You can disable to Validator using ValidatorEnabled property.

Alert box a good practice or not

I have a web form with more than 10 fields that are submitted to the database. So before submitting the values I am doing JavaScript validation.
Currently I am using JavaScript validation and shows an alert box if an error occurs in data entry.
Is it a good practice to show alert box when JavaScript validation fails or should I use asp.net validation controls to display the error messages?
I'd avoid using an alert box. It's annoying, requires an extra click, and since it's modal - stops the entire browser.Instead, highlight the erroneous fields/values, and print a message at the top, explaining that the highlighted fields need to be corrected before the user can continue.You can use Asp.Net validation, or jQuery form validation - both work equally well.
ASP.NET Validation controls are more recommended, because the alert can be intrusive. They can be set to give messages without posting back, which is ideal.
Just make sure you make it obvious when the form has failed.
from the usability perspective it is much better to use .net validation. Depending on your alert box implementation you may bombard user with many alerts which is very bad.
Also don't rely on client side validation only. Be sure to validate on the server side as well. And this is where .net validation might be handy again.
A good practice is what when your code supports fall back method. For example, if Javascript is disabled then the user should still be able to view the error messages, if any.
More over, they dont look as good as you could style your own divs and display them after some server side validation .. and then use javascript to hide/fade out this error message container div.
I think its a personal/design preference.
Sometimes its just more obvious when there is a javascript alert indicating something needs to be done. Sometimes a small red asterisk gets lost on the page.
In my own opinion, alert boxes are just too annoying to see on websites these days since they're all you see on Windows... But, if you prefer a better looking site, use something else. If you want to make sure the message gets across - then use an alert.

Resources