?
Solved

jquery validation

Posted on 2012-08-30
19
Medium Priority
?
681 Views
Last Modified: 2012-08-31
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");
0
Comment
Question by:erikTsomik
  • 9
  • 9
18 Comments
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38350882
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 38350915
I want to make sure the path provided to the user is correct. Do not need to check the directory
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38351228
any suggestions
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38351500
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
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38351529
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 38351560
I am simply typing the path. So the file is index.html and located in home I will type /home/index.html
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38351565
So you are just checking to see if the file exists in that path provided?
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38351583
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
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38351594
Ah, finally understand what you are asking for here...give me a few minutes and I'll get it ready for you.
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38351732
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 38351762
well good answer. But I do need the url. I need exactly that pattern
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38351880
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 38351886
SO how can i validate this \mycontent\1\index.html
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38351897
I DO NOT HAVE AN URL
0
 
LVL 13

Accepted Solution

by:
Andrew Derse earned 2000 total points
ID: 38351992
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|-|\.|_|~)|[!\$&'\(\)\*\+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[\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
 
LVL 19

Author Comment

by:erikTsomik
ID: 38355244
I've requested that this question be deleted for the following reason:

Please delete the question because I did not get any solution
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 38355245
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 38355571
I ask for regex but the regex provided does not work
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Find out what you should include to make the best professional email signature for your organization.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question