I have a CustomValidator, RequiredFieldValidator and ValidationSummary controls on an aspx page to check the current password and make sure the new password is not blank.
When I click submit I get the js alert() box from the summary control telling me about just the RequiredFieldValidator being false, but the client script for the CustomValidator does not seem to get its return value used. I have checked the "arguments.IsValid" value using FireBug, so it is getting called and returning the correct bool, but it never shows up in the val summary box?
Thanks,
Goosey
Its hard to say what is going on without the code. As a side note, you might want to check out the xVal framework as I think it handles what you are looking to do.
Related
I have a website in ASP.NET (WebForms, NOT MVC) which has a survey form divided in several slides. Each slide has a next button that, obviously does a transition (client-side, not post back or remote request) to the next slide.
In each slide I have several ASP.NET controls with their related validators. I want this validators to be triggered when I click the next button (or maybe when each input loses focus?).
I remembered ASP.NET doing client side validation on lost focus, but maybe I'm wrong... (I quit doing ASP.NET development about 3 years now, so I can't remember)
Thanks
UPDATE:
It would be better to make ASP.NET trigger each validator when the associated control lost focus. I remember ASP.NET doing this (or am I dreaming? =P)
First you need to make sure all of your validators have target controls specified using the "TargetControlID" Attribute on the validators.
Then you can set up a validation group per page and specify the group name in your next button and on the controls themselves.
If you are using regular expression validators you can choose them from this website
To Validate Client Side
If you are using custom validators you can create a client function and specify it on the custom validator using the ClientValidationFunction attribute and by setting EnableclientScript = "true" on the custom validator.
Just be sure your client function has sender and args parameters.
It looks like there's a supplied JavaScript function called Page_ClientValidate which should be callable to check the validation manually from JavaScript. I haven't used it, though, so YMMV.
put all your client-side validators into the same validationgroup and with your 'next' button add the same validation group. When you click the button it will automatically trigger all the validators before it does the post-back.
as to manually triggering the validation...
you might also be able to use ValidatorOnSubmit(). I remember doing this in another project but i'm having a hard time finding the code.
It seems that enabling 'SetFocusOnError' on each validator triggers the validation whenever I try to leave the field.
In short decorate your model, now Data Annotations are supported from Asp.Net 4.5
Check my Answer here..Client side webform validations
All ASP.Net client validation messages can be shown as an alert by setting the ShowMessageBox="True" property on the ValidationSummary control.
This works fine for anything that happens on the client.
For the custom validators that validate server-side I had assumed that what would happen is that when the page is returned to the browser, ASP.Net would inject some javascript to show the alert box. However this isnt the case.
If you had relied on the message box to show detail and just have a * next to the erroneous field (as per my clients req's) then it wont work as intended.
Does anyone have a solution for doing this? What I want is a way to possibly override the ValidationSummary control to inject javascript onto the page or something like this.
Thanks in advance.
You may use Page.RegisterStartupScript to show alerts after server-side validation.
I have a bunch of text boxes and a save button to update something. When I click Save I have code that determines whether they are correctly filled in, in the code behind file.
If they are not correctly filled in, I want to display an error message in the form of an alert.
What is the best way to do this? Pressing the button obviously makes the page postback, so I thought about adding something to the URL like mypage.aspx?errormessage=blahblah but I don't know if this is the best way or even how to do it...
Any suggestions?
Modal alerts are bad, as are postbacks. Try to check as much as possible on the client-side without a round-trip to server. See jQuery Validation plugins for a less intrusive way of validation.
Could you use a CustomValidator to trigger client side script that shows a alert box?
You could use the ClientScript.RegisterStartupScript() method in the server side error handling code to write a javascript snippet which calls alert('message'), something like this
private void ShowErrorMessage(string message)
{
string script = "alert('" + message + "');";
ClientScript.RegisterStartupScript(typeof(MyPage), "errorScript", script, true);
}
But I would recommend you use a validator instead. If you implement your own custom validator, you can make it emit client-side script which can run before the submit, to avoid the postback altogether.
A good thing about validators is that their error messages can be displayed in a ValidatorSummary on the page, avoiding the ugly alert box.
First of all I won't recommend showing an modal alert box to the user.
You can call a javascript function from the server side code and in that function you can pop out the error.
Or you can issue an AJAX request and after the validation on server side you can send back a response to the client.
ASP.NET's various validation controls (with client-side validation enabled), coupled with proper error messages and/or summary message will be good enough for most scenarios.
For 'AJAX feel and behaviour', put the controls into an updatepanel will be easy to implement too.
Use ye olde Validators as much as poss, they render out some javascript to the client so yu can do alot of validation using these controls and only when they are all satisfied does it allow the page to submit.
They do fire on every submit so if you don't want every submit action to fire them you make the controls part of a validation group.
They can validate input using regular expressions, make sure a field has a value and there is a few more as well.
Then there is property to declare a 'message' and a control to show all the validator messages. All very swish and built right into the IDE.
Go check out the validator controls.
Try the following code in your button click event:
string strErr="Error!";//Put your error message here
ClientScript.RegisterStartupScript(GetType(), "scrptName", "javascript: alert('"+strErr+"'); ", true);
It will show an alert message.
Else put a label on your aspx page and set visible false in page_load event.
When error occurs in your button event set the label visibility 'true' and fill the label text with the error message.
In ASP.net Webforms how do you detect which Textbox someone pressed enter?
Please no Javascript answers.
I need to handle it all in the code behind using VB.NET.
Why do you need to determine the which TextBox was pressed? Are you looking to see which TextBox was being focused so that you can trigger the proper button click event?
If you are looking to do something like this, one trick I've done was to "group" the appropriate form elements within their own panel and then set the "DefaultButton" property accordingly.
Doing this allows me to have a "Search by Name", "Search by Department", "Search by Id", etc. Textbox/Button combination on a single form and still allow the user to type their query parameter, hit Enter, and have the proper search method get invoked in the code behind.
I suspect it cannot be done without javascript - when you hit enter, the browser submits the form - it doesn't submit what field had the focus. So unless you use JS to add that information to the form being submitted, you're out of luck.
Without using Javascript, you just can't. That information is not conveyed from the client browser to the server.
As far as I know there is no possible way for a server side script to detect that. It simply does not get sent to the server. It must be done client-side (i.e. With Javascript) and then sent to the server.
I solved this for one site's search by looking at the Request.Form object, server side to see if the search box had a value. I did it in a base class that all my pages (or a base class for the masterpage) inherit from. If it has a value, the odds are pretty good somebody typed something in and hit enter and so I handled the search.
In the event handler, the "source" object (the first parameter of the event handler) is the object raising the event. Type it to button and get the name, or use reflection to get information out of the non-typed object.
In addition, if the control is a child of a web control that you do not have raise its own events (just saying...) then you can use OnBubbleEvent to determine what's going on. OnBubbleEvent also has a "source" parameter you can type, or use reflection on.
I am using the open source Javascript WYSIWYG from OpenWebWare and Asp.Net RequiredFieldValidator on the TextBox which I am calling the WYSIWYG for. Everything works fine, but the first time I try to submit the form, I get the server-side RFV ErrorMessage "Required", but if I submit a second time, it goes through.
Am I missing something? I would like to have the client-side validation... how can I get the text to register as not empty?
I think the reason for this behavior is that validation code runs earlier than the code that updates underlying TextBox from value of WYSIWYG. So the first time you get the error, then the field is updated and the second time you don't get it. Try removing all the content the second time and I bet you wont get validation error (since the value for validator at the moment is what you actually submitted the first time).
The solution would be to find a JavaScript API call for your WYSIWYG which would force the update of the underlying text box field and call it onclick (client-side) of your submit button or whatever you use for that.
the textarea HTML tag is one of the most unpleasent tags to work with and I'm not 100% sure if the client-side validator will support it, regardless of whether it's a WYSIWYG or not.
I think you'd be best off using a CustomValidator and writing the JavaScript which does the checking manually.
Alternatively you can debug though the JavaScript which is used with FireBug or VS 2008.