-1

I am getting extension name from user to save in db in Asp Form . How i can validate that user enter extension name with .(Dot) and without .(Dot) name generate Error.

  • you can ask user without dot and then add dot yourself? Please be aware that multiple file extensions such as `file.ext1.ext2` may cause you trouble. – mcy Sep 30 '15 at 12:26

3 Answers3

1

Do you want to validate the data as the user is entering it on the form, before submitting? If so, you may be looking for a RegularExpressionValidator. See: https://msdn.microsoft.com/en-us/library/eahwtc9e(v=vs.71).aspx and http://asp.net-tutorials.com/validation/regular-expression-validator/ then use one of the Regular expressions that Webruster and Ahsan provided you.

Sample:

<asp:TextBox runat="server" id="txtExt" />
<asp:RegularExpressionValidator runat="server" 
          id="rexDot" controltovalidate="txtExt" 
          validationexpression="^[.]([a-zA-Z0-9]+)?$" 
          errormessage="Please enter a Dot!" />
Joe
  • 1,091
  • 1
  • 11
  • 23
  • exactly. @Joe by using REGEX i validate my text box string and i also use the same expression as you write in comment.. Thanks for it... – Ahsan Pervaiz Oct 01 '15 at 10:40
0

By using RegEx I can do it.

Regex.Match("String to Compare" , @"^[.]([a-zA-Z0-9]+)?$")

I get user input in a string and compare it with this Validation using RegEx.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
0

Regular expression to validate file formats for .mp3 or .MP3 or .mpeg or .MPEG or .m3u or .M3U

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$/;

Regular expression to validate file formats for .doc or .docx

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;

Regular expression to validate file formats for .txt or .TXT

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.txt|.TXT)$/;

Regular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/;
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48