1

So I have this OPTIONAL field in my application form which is a Filename for an xml file, but since it is optional I just need it to apply the regular expression validation ONLY WHEN SOMETHING IS TYPED, otherwise if its empty don't apply it and let the user proceed without specifying the Filename.

                <asp:TextBox id="tbxmlFile" runat="server" Width="100px"></asp:TextBox> &nbsp;
                <asp:regularexpressionvalidator id="regular2" Display="Dynamic" controltovalidate="tbxmlFile" runat="server" errormessage='"Filename must only contain letters and numbers and end in .xml' validationexpression='([a-zA-Z0-9\s\.])+(.xml)$' ForeColor="Red"></asp:regularexpressionvalidator>

Is there any regular expression that let me do this? or is there any way to insert the regularexoressionvalidator from the VB backend if the string of the textbox is empty? Thanks for your answers

The following is the regex im using: ([a-zA-Z0-9\s.])+(.xml)$

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Retrosec6
  • 169
  • 11

1 Answers1

0

You can add or expression for empty

([a-zA-Z0-9\s\.])+(.xml)$|(?!)

Or

^$|your_pattern 

Explanation

  • (?!) Specifies a group that can not match after the main expression (if it matches, the result is discarded).
  • ^ and $ are the beginning and end of the string anchors respectively.
  • | is used to denote alternates, e.g. this|that.
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46
  • Thanks answer and explanation this really helped, I didn't know you can apply operators like or in regular expressions, do you know if there's any documentation about it and more expressions like (?!) to see what their mean and learn how to use them? – Retrosec6 Mar 02 '20 at 18:58