0

I have an ASP.NET Web Forms application.

I have a Form with various TextBoxand right now I have several asp:RequiredFieldValidator linked to them belonging to the same ValidationGroup.

Now I have to apply to some TextBox an additional validation with related error message. In the specific I got to check whether the text inside the TextBox is a Guid or not. Morevoer this check has to be done on the fly, meaning that as soon as the user moves the cursor from the TextBox the validation has to be performed, without the need to press submit.

  1. How can I call the IsGuid(string guid) function from Javascript?

  2. How can I attach two different error messages as validation (for instance I want to be displayed a message if TextBox.Text has carachters not allowed and lenght < N)

  3. Is it easier to implement this validation with jQuery or with the ASP.NET validators?

If somebody else has any other idea for its implementation, please feel free to propose. Thanks!

2 Answers 2

1

You can use ReqularExpressionValidator control.

Here a regex

^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$
Sign up to request clarification or add additional context in comments.

4 Comments

thansk, I use the same Regex already in my server-side IsGuid function. Is there any way to use AJAX to asynchronously send the text in the TextBox to the fucntion?
You don't need to use server code. The RegularExpressionValidator control is from the same area as RequiredFieldValidator. Do you have any reason for not using it?
no but just in case I need another validation where I should implement more complex business rules, maybe with DB access. In that case I'd need a server fucntion to be called from JavaScript
For complex rules, use CustomValidator. To access server side code, you need to use AJAX. jQuery is a good tool to easy the things
0

You can apply as many Validators as you need to the same control, in this case your TextBox.

In this scenario, a Custom Validator is the way to go because it enables you to do the validation with any function you develop to cover your needs. Please, have a look at this simple tutorial.

UPDATE 1: Server Side Validation

This is how it calls the server side function in the declaration of the CustomValidator:

<asp:CustomValidator runat="server" id="custPrimeCheck"
    ControlToValidate="txtPrimeNumber"
    OnServerValidate="PrimeNumberCheck"
    ErrorMessage="Invalid Prime Number" />

Example of the "PrimeNumberCheck" VB function:

Sub PrimeNumberCheck(sender as Object, args as ServerValidateEventArgs)
Dim iPrime as Integer = Cint(args.Value), iLoop as Integer, _
    iSqrt as Integer = CInt(Math.Sqrt(iPrime))

For iLoop = 2 to iSqrt
  If iPrime mod iLoop = 0 then
    args.IsValid = False
    Exit Sub
  End If
Next

args.IsValid = True
End Sub

UPDATE 2: Client Side Validation

This is how it calls the server side function in the declaration of the CustomValidator:

<asp:CustomValidator runat="server" id="custPrimeCheck"
    ControlToValidate="txtPrimeNumber"
    ClientValidationFunction="CheckPrime"
    ErrorMessage="Invalid Prime Number" />

Example of the "CheckPrime" JavaScript function:

function CheckPrime(sender, args)
{
    var iPrime = parseInt(args.Value);
    var iSqrt = parseInt(Math.sqrt(iPrime));

    for (var iLoop=2; iLoop<=iSqrt; iLoop++)
      if (iPrime % iLoop == 0) 
      {
          args.IsValid = false;
         return;
      }

    args.IsValid = true;
}

Thanks to @AdrianIftode for making me aware of this.

11 Comments

This server side function is called.. server side. It won't be called on client. You need to create a javascript function which checks for a prime number.
@AdrianIftode I may be confused, but as far as I know this answer responds this question: How to apply Client-side validation by calling a server-side function in ASP.NET. Right?
It doesn't completely respond. You can call server side code, but not with a custom validator. Check for AJAX, WebMethods. A custom validator needs also a javascript function. That function will be able to call server side code using AJAX.
@AdrianIftode Please, have a look at the tutorial I recommended: 4guysfromrolla.com/articles/073102-1.aspx since it does not concur with your argument
@AdrianIftode I have just updated my answer. Thank you. Regards,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.