jquery validation

I have a validation that validate the filename like this index.html. How would i change the validation so it will also check for a directory provided . For example /html/index.htm

$.validator.addMethod("filename", function(value, element) {
                return this.optional(element) || /^[a-z]\w*\.(html|htm|swf)$/i.test(value);
            }, "Please specify a valid file name");
LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?
 
Andrew DerseConnect With a Mentor IT ManagerCommented:
Ok, so now you need to validate it both ways:

/home/index.php

AND

\home\index.php?

-------------------------------------------------------

Here's my test code...php and js in one...for just the /home/index.php like you original post here where you said "No, All i do is checking if the entered file is in correct format /home/index.html or /home/test/index.html and so on"  If you need both, let me know...the code is much simpler.

<?php

if(isset($_POST['filename'])) {
	$filename = $_POST['filename'];
?>
<script type="text/javascript">

var myVariable = 'http://<?php print $filename;?>';
if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(myVariable)) {
  alert("valid input");
} else {
  alert("invalid input");
}
</script>
<?php
} else {
?>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="POST" >
	<input type="text" name="filename" id="filename" /><br />
	<input type="submit" name="validate" id="validate" />
</form>
<?php
}
?>

Open in new window

0
 
Andrew DerseIT ManagerCommented:
Let me just get this question straight...you want to be able to see if the file is in the correct folder?

Why would you need to know which folder it's in...?  Just need a little more clarification, thanks.
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I want to make sure the path provided to the user is correct. Do not need to check the directory
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
erikTsomikSystem Architect, CF programmer Author Commented:
any suggestions
0
 
Andrew DerseIT ManagerCommented:
I can only think of getting the URL path from the url and validating that...using php...not jQuery...I'm still thinking on how this can be accomplished...hoping someone else chimes in.
0
 
Andrew DerseIT ManagerCommented:
I guess I am still unclear on what the overall picture is, this is just a small snapshot of what you are trying to do...but there are parameters you are passing around we can't see where they are coming from or what you are using them in.

Is a user filling out a form on your site?  Are they uploading a file or simply typing in a textbox...etc...

More details on the over all scope of the issue would be nice to help decifer this.
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I am simply typing the path. So the file is index.html and located in home I will type /home/index.html
0
 
Andrew DerseIT ManagerCommented:
So you are just checking to see if the file exists in that path provided?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
No, All i do is checking if the entered file is in correct format /home/index.html or /home/test/index.html and so on
0
 
Andrew DerseIT ManagerCommented:
Ah, finally understand what you are asking for here...give me a few minutes and I'll get it ready for you.
0
 
Andrew DerseIT ManagerCommented:
Truly a good question...I can only get it to work with this code if the http:// is in front...
<script type="text/javascript">
var myVariable = '<?php print $filename;?>';
if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(myVariable)) {
  alert("valid url");
} else {
  alert("invalid url");
}
</script>

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
well good answer. But I do need the url. I need exactly that pattern
0
 
Andrew DerseIT ManagerCommented:
What if you just stripped out the http:// after it validates?  That way you can use the script above...validate it...if validated, strip out http://...

Or...simply add the http:// to the string before it gets sent to the validator?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
SO how can i validate this \mycontent\1\index.html
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I DO NOT HAVE AN URL
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I've requested that this question be deleted for the following reason:

Please delete the question because I did not get any solution
0
 
Andrew DerseIT ManagerCommented:
I have provided a solution for erikTsomik that fits his original request even though he says he doesn't have a url...he's typing in a url...whether he knows it or not...and my solution checks to see if it's a valid url.

I see he's opened up a new question based off of some of the information I provided in this question.
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I ask for regex but the regex provided does not work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.