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!

CiccioMiami
  • 8,028
  • 32
  • 90
  • 151

2 Answers2

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}$
Community
  • 1
  • 1
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • 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? – CiccioMiami Apr 25 '12 at 13:29
  • 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? – Adrian Iftode Apr 25 '12 at 13:31
  • 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 – CiccioMiami Apr 25 '12 at 13:45
  • For complex rules, use CustomValidator. To access server side code, you need to use AJAX. jQuery is a good tool to easy the things – Adrian Iftode Apr 25 '12 at 13:47
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.

aleafonso
  • 2,244
  • 8
  • 38
  • 58
  • 1
    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. – Adrian Iftode Apr 25 '12 at 14:14
  • @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? – aleafonso Apr 25 '12 at 14:29
  • 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. – Adrian Iftode Apr 25 '12 at 14:31
  • @AdrianIftode Please, have a look at the tutorial I recommended: http://www.4guysfromrolla.com/articles/073102-1.aspx since it does not concur with your argument – aleafonso Apr 25 '12 at 14:39
  • Well, actually it says "try entering a nonprime number, such as 6, into the TextBox - you are not alerted to the fact that the number you entered is not prime until you submit the form" – CiccioMiami Apr 25 '12 at 14:42
  • 1
    @AdrianIftode I have just updated my answer. Thank you. Regards, – aleafonso Apr 25 '12 at 14:50
  • @aleafonso, I read your article and your answer. You actually do not call any server-side function from the Javascript – CiccioMiami Apr 25 '12 at 15:03
  • @CiccioMiami Once you know how to call the JavaScript function, you need to know how to call your codebehind. These are separate problems. The following links might be helpful: http://stackoverflow.com/questions/7158055/asp-net-call-webmethod-from-javascript-asyncronous, http://www.codeproject.com/Articles/180355/Calling-a-code-behind-function-from-JavaScript, http://stackoverflow.com/questions/5828803/how-to-call-code-behind-server-method-from-a-client-side-javascript-function – aleafonso Apr 25 '12 at 15:27
  • @CiccioMiami and if my answer was any useful at all, please upvote – aleafonso Apr 25 '12 at 15:43
  • @aleafonso: sorry but you just explained how to use the CustomValidator. I know already that, I want to know how to call a server function from Javascript – CiccioMiami Apr 26 '12 at 08:47
  • @CiccioMiami cool! So, create the right question for it. My most sincere apologise for any inconvenience caused – aleafonso Apr 26 '12 at 08:52