Reputation: 8256
I have an ASP.NET Web Forms application.
I have a Form
with various TextBox
and 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.
How can I call the IsGuid(string guid)
function from Javascript?
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)
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!
Upvotes: 0
Views: 1685
Reputation: 2256
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.
Upvotes: 0
Reputation: 15663
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}$
Upvotes: 1