Results 1 to 10 of 10

Thread: [RESOLVED] Validate string

  1. #1

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    [RESOLVED] Validate string

    My app has an input tag to allow a file to be saved. I need to validate the file name. The file naming convention needed is:

    12345-results.pdf

    in lowercase. Any suggestions?
    Last edited by EyeTalion; Mar 4th, 2009 at 08:26 AM. Reason: RESOLVED
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Validate string

    Hey,

    Why do you need to validate the file name?

    Can you not just change the file name to whatever you need it to be when you save the file on the server.

    Gary

  3. #3
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: Validate string

    You can split up the string into separate parts and validate each part. For instance, you can strip out the first five characters and then do something like:
    Integer.TryParse (first5, first5final) or something along those lines.

    But then, do the numbers have to be in sequential order? Can the number be 82345? If some sort of sequential order must be maintained, then it gets real tricky. I couldn't even begin to think of how to validate that the number is in order ala '12345'. I'm sure there's a way though.

    Two other words come to mind, related to your problem, regular expression.
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  4. #4
    Junior Member
    Join Date
    Feb 2009
    Posts
    26

    Smile Re: Validate string

    Does the file name strictly have to be 12345-results.pdf? That makes it pretty straight forward. Suppose you have an input tag like

    Code:
    <input type="text" onblur="return regexmatch()" id="mytextbox" />
    Your javascript function would look like

    Code:
        function regexmatch()
        {
          var reg =new RegExp('12345-results.pdf');
          var val = document.getElementById('mytextbox').value;
          if(reg.test(val))      
            return true;
          else
          {
            document.getElementById('mytextbox').focus();
            alert("File Name not in proper format !!!");
            return false;        
          }
        }
    Here once the focus is on the text box the javascript function would allow the focus to move away from the text box only if 12345-results.pdf is entered into it. I am not sure I have understood you correctly . Could you explain a bit further.

    Warm Regards,
    sr_jay.
    Quote Originally Posted by EyeTalion
    My app has an input tag to allow a file to be saved. I need to validate the file name. The file naming convention needed is:

    12345-results.pdf

    in lowercase. Any suggestions?

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Validate string

    Hey,

    The one thing about doing client-side validation of the server name is that if someone wanted to, that check can be easily circumvented. The only real test would be to validate the file name again at the server side. Client side validation is really only going to work for "honest" users.

    Gary

  6. #6
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: Validate string

    As Gary stated, sr_jay's solution will only work for certain users. What if someone has javascript disabled in their browser not because they want to hack sites, but because they just want it disabled? The user is not purposely trying to harm the site, however they can enter bad information on accident.

    All the OP needs to do is use a regular expression validator control. It will perform the same validation as the javascript code, but since the control is server-side, it's a bit harder to circumvent.

    Unfortunately, that's about all I can do to help, because EyeTailon has not given enough information. He says that '12345-results.pdf' is a naming convention, which is of little use to me. I can only ASSume that it has to be 5 numbers, a dash, a string, and a .pdf, but I could be completely wrong and it has to be 12345, then a string name, then a .pdf, or I could be wrong even still and it's something else.
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Validate string

    Try this, expand to fit your needs. I'm just assuming a number followed by a name followed by strictly alphabetical extension (no MP3).

    [1-9]+-[a-z]+\.[a-z]+

    And I'll also emphasize that it should be both client side and server side.

  8. #8

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    Re: Validate string

    thanks guys for all the input...I've decided to use Geps solution and name the file myself, since I have all the info at the time of the save.
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Validate string

    Hey,

    Glad to hear you got a solution that works for you.

    Remember to mark your thread as resolved if your question has been answered.

    Gary

  10. #10

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    [RESOLVED] Re: Validate string

    Resolved
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width