• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

jQuery Validate Custom RUle

Hi all,

I'm trying to create a simple custom rule for the jQuery Validation plugin, I know the syntax:

jQuery.validator.addMethod("greaterThanZero", function(value, element) {
    return this.optional(element) || (parseFloat(value) > 0);
},

But my javascript programming just isn't good enough!  I'm basically validating a folder path and just want to ensure that the folder path has a slash at the end and at the beginning (e.g.  /spandex-goat-socks/   ).  Obviously I can do this server side easy enough but I'm trying to have both serverside and clientside validation for a better experience.  

Can anyone help?

Thanks

Bob
0
Mango-Man
Asked:
Mango-Man
  • 4
  • 2
1 Solution
 
leakim971PluritechnicianCommented:


jQuery.validator.addMethod("goodPath", function(value, element) {
    return this.optional(element) || (/^\/.*\/$/.test(value));
},
0
 
leakim971PluritechnicianCommented:
if your object in an input file, don't loose your time, it will not work, you can't read the file path with javascript (there's a hack for Internet Explorer but forget it)
0
 
Mango-ManAuthor Commented:
Hi Leakim971,

Many thanks for the assistance.  Out of curiosity, what does test(value)); do in the code you have given?

The object is an input file (hence the use of the jQuery validation plugin) but I'm not checking to see if it exists on the server, I just need to know it's a correct relative path (e.g. /images/) and then when the form is submitted, the server side script creates the folder.

Bob
0
Technology Partners: 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!

 
leakim971PluritechnicianCommented:
>what does test(value)); do in the code you have given?

it test the given value to see if it's conform a regular expression : ^\/.*\/$
this regular expression say :
- a string starting by << / >> : http://www.w3schools.com/jsref/jsref_regexp_ncaret.asp
- followed by one or more than one character : http://www.w3schools.com/jsref/jsref_regexp_dot.asp & http://www.w3schools.com/jsref/jsref_regexp_zeromore.asp
- ending by << / >> : http://www.w3schools.com/jsref/jsref_regexp_ndollar.asp

<< \ >> before << / >> allow we to escape it (/) because it's the char delimiting of our regular expression

More infos : http://www.w3schools.com/js/js_obj_regexp.asp

>The object is an input file (hence the use of the jQuery validation plugin) but I'm not checking to see if it exists on the server, I just need to know it's a correct relative path (e.g. /images/) and then when the form is submitted, the server side script creates the folder.
I understood your need. There's only a solution for IE
0
 
Mango-ManAuthor Commented:
Hi Leakim,

Thanks - I understood the regex, just not the other bit.  The code above kept giving me errors so in the end I created a generic regex addition to the jQuery validation plugin:

$.validator.addMethod("regex", function(value, element, regexp){var re = new RegExp(regexp); return this.optional(element) || re.test(value);}, "Please check your input.");

And called it like this:

$("#articlesFolder").rules("add",{required:true, regex:"^\/.*\/$", messages:{required:"You must specify a folder in which each articles folder will be created."}});

You provided a solution however so points awarded!

Thanks

Bob
0
 
leakim971PluritechnicianCommented:
you're welcome, thanks, have fun!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now