Calling Asp Validator from Javascript - c#

I need to call a validator to show an error message from JavaScript
For Example:
I have a textbox in which i take time as input and a Button to submit the value.
I have a minimum time and maximum time.
I'm validating the time textbox using JavaScript and using an alert message to show if there is an error in the time entered
Now instead of showing the alert box I want to call the validation callout extender
to show the error message
Can you help me out?

Have you tried using ASPX validators, these solve javascript and also server side parts.
You can then just ask page.IsValid.
here is some more information about ASPX validator controls
Validators are pretty easy to use, as they support also regular expressions and own functions. But sometimes you've to use two validators on one field. e.g. requiredFieldValidator and Compare validator
http://asp.net-tutorials.com/validation/required-field-validator/
http://msdn.microsoft.com/en-us/library/bwd43d0x.aspx
http://msdn.microsoft.com/en-us/library/debza5t0.aspx

Related

how do I get the validationsummary text to put in a custom alert ASP.NET

for a recent schoolproject I need to make an ASP-website.
On the loginform I have a few validators and a validationsummary.
The summary itself works, but I would like to get the text from it,
so I could put it into my own custom "popup". The popup is just a twitter-bootstrap model wich I tweaked a bit so I can put in a title and some text easily from any code behind file.
Can anybody help me? Any c# code behind, or jquery solution is fine to me.
Try the solution in this answer:
Can I run some custom script after ASP.NET client side page validation fails?
Basically you call the Page_ClientValidate("") function and write custom JavaScript to display the alert

How to show validation errors after a postback

I am having some textboxes and dropdownlist controls but if i select the ddl value the validation errors are disappearing and after the button click they are reappearing but i want to show the errors even after a postback how can i do this??
Could you post your code so we can perhaps see what the specific issue might be? Without seeing any code, I would say try adding this into the Page_Load function:
if (IsPostBack)
Page.Validate();
Alternatively, add this to your DropDownList or whatever controls are initiating the postback:
CausesValidation="true"
.. as per the answer here: Validators do not Validate after postback occurs
Please remove your asp.net validators if added with the controls and also remove any client side validation in Java script. Now add validation code on the page you are redirecting to. If that validation fails redirect back to the controls page with proper messages to be shown
If you dont like the default behavior - don't use validation controls and implement them by yourself using client side programming.

How to change Label value in MVC2

In my application (written in c#) I have a textbox and a submit button.
If the user clicks "submit" without entering any value, I want him to stay at the same page and display a message saying "Please insert value"
I thought to put a label, and then just edit its value and/or visibility.
The structure of my application is as following:
Controller A
inside View, under A, I have B.aspx
and inside B.aspx I call a partial C.ascx
The submit form with my label are inside C.ascx
How can I change the lable value and/or visibility?
You should use the Data Validation Attributes for this.
Something like this:
[Required]
String textBoxValue{get;set;}
This will create a custom javascript/jquery file that will perform the appropriate validation for you.
You can even change the default error message for Required.
[Required(ErrorMessage = "The textbox value is required because I said so :)")]
ASP.NET MVC 2 support both server-side and client side validation. I believe this version takes a dependency on the jQuery Validation plugin. In your view, you can use the Html.ValidationMessage or Html.ValidationMessageFor helper methods that will allow your application show error messages whenever validation fails
Here's a link to a post that may be of further assistance.
http://goo.gl/Jxozv

Refreshing the asp.net web page after validation

I have an asp.net web page (C# 2008) where the user would enter an EmployeeID, and when they tab out of the textbox (the page executes a validation code block in the codebehind), they get a messagebox prompting them to select one of two values from a dropdown listbox.
The code for the message prompt in the codebehind is :
Response.Write("<script>window.alert('Please select Alpha or Beta')</script>");
After the prompt is displayed, and the user clicks "ok" and returns to the page, the text on the page appears distorted (the text in labels are a size larger, the labels get wrapped to another line etc)
I tried putting a Response.Redirect("UserProfileMaint.aspx"); after the messagebox in the codebehind, but now, the messagebox does not appear;
So this is my squence:
User enters EmployeeID
If user has NOT selected Alpha or
Beta, then show messagebox
If user HAS selected Alpha or Beta,
then don't show messagebox
I want to display the messagebox validation, and ensure the appearance of the text on the page is not distorted. How can I do this?
Response.Write writes directly to the output stream, placing it before <html> which the browser gets very confused by (causing your text issues). What you want to do instead is this:
ClientScript.RegisterStartupScript(GetType(), "alert",
"alert('Please select Alpha or Beta');", true);
ClientScript.RegisterStartupScript includes the script in the page to be run on load rather than put in the response too early. The last argument: true is telling it to wrap that alert in <script> tags, keeping your code-behind cleaner.
The best way to handle this would be to use Javascript and do Client side validation. If you really want to do server side validation then instead of showing a alert by using Response.Write you should use RegisterStartupScript or better show the message using a Label at the top.
HTH
When you call Response.Redirect, that occurs on the server side, whereas you want the redirect to occur on the client side after a choice is made.
To do that, when you write your script with Response.Write (btw, there are much better ways to do this), you would have logic that determined what the user selected, and then based on the selection, either requery them for data, or set the location property of the inherent document object to the url you want to redirect to.

How can I validate that several textboxes contain data when I click submit?

I have 10 defined textboxes with strings.
I have to check all if they are not empty while clicking ok button
whats the cleanest way to check them all and when function is at end. each checkbox which was empty to give this a specific CSSclass. perhaps. ClassError. ( which highlights red)
I'm happy for answers.
I would add RequiredFieldValidators to them, as well as a ValidationSummary control.
edit: You can also add fancy AJAX effects with the ValidatorCallout from the AJAX toolkit.
edit: Validator controls also support client-side validation.
Using javascript or C#?
Javascript I will create an array of textbox and loop through it.
C# just go FindControl within a Panel or the Container of the text box and go something like this
foreach(Control C in ContainerID.Controls)
{
if ( C is TextBox )
{
if ( String.IsNullOrEmpty((C as TextBox).Text))
{
// Do things this way
}
}
}
Something like this would work on the client side using jquery:
$('input').filter(function(){return this.value=="";}).css("CSSclass");
edit: I just saw the C# tag, I'll leave this here for posterity though.
What you're doing might be simple enough to do easily with some custom javascript code, though I would say that in general the built in validator controls are both easier to use and more robust than a simple validation routine that someone might write. In addition to client-side validation, the validator controls also perform server-side validation to ensure that the data submitted is truly valid, in case someone has javascript disabled in their browser.
If validator controls are included on the page, then they will include some javascript functions that you can invoke, such as Page_ClientValidate(). I believe this will return a boolean telling you whether validation passed and will trigger the visual indicators that identify what the errors are. You can execute Page_ClientValidate('') to trigger only a group of validator controls; actually I think you must do that in order to trigger validation on any validator controls that have a value in their ValidationGroup property, as I don't think Page_ClientValidate() will trigger their validation logic.
There is a CustomValidator control that you can point to your own client-side function if you want, in case you do have some special validation logic that you can only implement through a custom javascript function. This is nice to use, because then your custom javascript function will be executed by the built-in validator framework along with any other built-in validator controls that you might choose to include on the page.
Side note regarding client-side validation: I suggest that you avoid doing the following to trigger validation:
onclick="return myValidationFunction();"
because if there is any other javascript code being injected into the onclick event, your return statement will prevent it from executing. So instead, I suggest doing this:
onclick="if(!myValidationFunction()) return false;"
That's bitten me enough times that I thought I'd just throw that out there. This problem is particularly noticeable if you have an ASP.Net button on which you've set the UseSubmitBehavior property to false, as it will cause the button to render as an HTML "button" control instead of a "submit" control, and as a result, executing a return statement, either true or false, will prevent the button from triggering a postback.

Categories

Resources