Realtime validation in database with custom field validator - asp.net

I am creating a registration form in asp.net and I am trying to figure out how to check if the requested username is available upon lostfocus of the text field.
I setup a custom validation field but I can't seem to make it work on lostfocus.
Is there a way to do it?
What should I read?
Thanks

well you can check the username exist or not in custom validator. just check the url below. IT checks the user is exist or not.
Attach a custom validator to your textbox that hooks up to a validation method server side.
Using javascript, you'll need to hook up an eventhandler to the focusout event and make an ajax call. That call will send the value back to the server and get a response of valid or not.
http://brian.dobberteen.com/code/jquery_ajax_custom_validator/

Related

How to disable AJAX sending method in Contact Form 7 and keep other JS options like validation

I would like to use wpcf7_form_action_url filter and change action in form.
To be able to do this, I need to disable the AJAX which is used when submitting the form
define('WPCF7_LOAD_JS', false) is not a solution because WPCF7 AJAX is packaged into an entire JS flag - so this will also turn off JS validation
if WPCF7_LOAD_JS JS is disabled, it will take the input to the server, validate and return to the form and show the errors. So, I think you not losing any validation functionality when submitting the form.

How to trigger ASP.NET client-side validations without submit?

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

How does RequiredFieldValidator automatically stops database being updated?

My previous impression of RequiredFieldValidator and similar have been that they show you an error label and set Page.IsValid false and that is it. Meaning they leave the rest of the task (preventing the use of wrong input data) to you. But today I have realised say if you use a DetailsView to insert a new record to a database, and you use validators to check the TextBoxes inside the DetailsView, they automatically prevent the database from being updated.
I would like to know how this is implemented behind the scene. I'm guessing it aborted the Page Lifecycle at Validator.PreRender event, so that database connections at later stage could not be reached? I'm probably wrong.
I'm trying to use Reflector to get inside the RequiredFieldValidator to see how it is implemented, but I don't really know where to look. Can someone give some hints?
This article explains the validation in detail for ASP.NET.
http://msdn.microsoft.com/en-us/library/aa479045.aspx
Validation can be both Server side and/or client side. If used, client side validation doesn't let user submit the form until it is validated. Client side validation is implemented using JavaScript and DOM. Every submit button is wired by the framework to check validation before doing post back.
Server side validation works differently. The event chain is extended between page load and event procedure call. The validation results are set, which can be interpreted by any event procedure.
In your situation I guess you have client validation on - which it prevents Form from being submitted to server and that's why no actual update to database is done.
EDIT: DetailsView control does support server side validation controls. Follow this link for details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx#security

InfoPath Submit functionality

I need to save my InfoPath form regardless of whether the required fields have been entered when the user clicks on a button. Is there a way to turn off validation for the InfoPath form temporarily?
By save i assume that you mean that you want to save the xml data file locally rather than submit your form to a datastore (webservice, sharepoint etc)
In tools>form options you can enable "save/save as" on the form (i think this is on by default) it will complain that there are validation errors but still allow you to save.
There is also save via code-behind using which will not go through the validation routine but will require that the form is fully trusted as it needs access resources that Infopath forms are normally restriced from using.
VB.Net
Me.SaveAs("c:\temp\myfile.xml")
C#
this.SaveAs("c:\temp\myfile.xml");
Or as Chris suggested allow nulls as part of your data source.
In my form, I required a button ("Save As Draft") that would submit the form to a SharePoint library even when validation errors were present.
This code-behind function worked for me:
public void SaveAsDraftButton_Clicked(object sender, ClickedEventArgs e)
{
this.Errors.DeleteAll();
this.Submit();
}
I've done similar by including radio buttons on the form with the labels 'I have completed this form and am ready to submit' and 'I am not done with this form. Save it and finish later'. Each validation then look something like (validation OR saveOptions == 'complete'). The submits similarly use this field to decide what actions to take.
You will have to write your own validation code in the code behind file and have it validate when the user clicks the submit button or not when you just want to have the code.
Another approach is to have the fields allow null values and only validate when they have text in them. This method would not require custom code.

Advanced Search Form Validation in asp.net

I'm looking to make validation for a page in which one or more fields have a value in them. I have an advanced search form in asp.net and I'm trying to pop up an error and not post back if all the fields are empty. I've looked into required fields validators but I'm not sure how to make them work together in a AND type fashion instead of the OR fashion that a validation group of required field validators would imply. I hope this makes sense. Thanks for the help.
i had to do something similar years ago and i was using 1.1 then.
what we ended up doing was creating required field validators but disabling them.
then onload we would loop through the validator dictionary, enable them and check if they passed. if any of them passed we broke off the loop and we continued execution, otherwise, if all of them failed then we displayed a warning. Unfortunately, this would require a postback.
If you want to accomplish this on the client-side then you could write a simple javascript function to take care of it before postback.
for every control, place an onBlur event. javascript will check if there is a value in the field and maintain a flag. then before submission you would check the flag and either allow submission or show warning.
You could just write the javascript validation function yourself to handle this case and attach it to your search button.

Resources