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?
Printable View
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?
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
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.
Does the file name strictly have to be 12345-results.pdf? That makes it pretty straight forward. Suppose you have an input tag like
Your javascript function would look likeCode:<input type="text" onblur="return regexmatch()" id="mytextbox" />
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.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;
}
}
Warm Regards,
sr_jay.
Quote:
Originally Posted by EyeTalion
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
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.
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.
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.
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
Resolved