Avatar of erikTsomik
erikTsomik
Flag for United States of America asked on

validate file name using jquery

I need to validate a file name. For example index.html. It need to be done as addvalidator function.
No back slashes are allowed so /index.html is not valid
jQueryJavaScriptAJAX

Avatar of undefined
Last Comment
Jon Norman

8/22/2022 - Mon
Proculopsis

Try something like this.
erikTsomik

ASKER
how would I add it to a add.validator function
darren-w-

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Jon Norman

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
erikTsomik

ASKER
JonNorman

the code does not work. basically no validation is triggering
Jon Norman

Hi erik, you are either not using the plugin http://docs.jquery.com/Plugins/Validation or you are not then calling the method you have setup using validator.addMethod.
jQuery.validator.addMethod("filename", function(value, element) { 
  return this.optional(element) || /^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\";|/]+$/g.test(value); 
}, "Please specify a valid file name");
$("#myform").validate({
  rules: {
    fileName: "filename"
  }
});

Open in new window

Have a look at the documententation, this will show you how to set-up custom validator methods and look at the built in methods for how to use them.
erikTsomik

ASKER
I am using the method but I am  calling the method like this
rules: {
    fileName: "true"
  }
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jon Norman

that to me says that the field with the name "fileName" is looking for a method called "true" to do the validation, if your field is named "fileName" you should do:
rules: {
    fileName: "filename"
  } 

Open in new window

Where filename (all lowercase) is the name of the method you have added, and fileName (java case) is the name of your field.
erikTsomik

ASKER
the reason I am asking is I need to do this validation conditionally, so if for example var x = 1 the validate filename other wise do not
Jon Norman

Hi Erik,

I suggest that in the method you add you should do the conditional check:
jQuery.validator.addMethod("filenameOrxEquals1", function(value, element) { 
  return this.optional(element) || x==1 || /^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\";|/]+$/g.test(value); 
}, "Please specify a valid file name");
$("#myform").validate({
  rules: {
    fileName: "filename"
  }
});
...
rules: {
    fileName: "filenameOrxEquals1"
  } 

Open in new window

this sets up the method called filenameOrxEquals1 and then uses it in the rules for the fileName field.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes