Avatar of erikTsomik
erikTsomikFlag 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
Avatar of Proculopsis
Proculopsis

Try something like this.
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

ASKER

how would I add it to a add.validator function
ASKER CERTIFIED SOLUTION
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

ASKER

JonNorman

the code does not work. basically no validation is triggering
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

ASKER

I am using the method but I am  calling the method like this
rules: {
    fileName: "true"
  }
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

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
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo