We help IT Professionals succeed at work.

validate file name using jquery

erikTsomik
erikTsomik asked
on
478 Views
Last Modified: 2012-06-27
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
Comment
Watch Question

Try something like this.
erikTsomikSystem Architect, CF programmer

Author

Commented:
how would I add it to a add.validator function
Information Systems Manager
CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
erikTsomikSystem Architect, CF programmer

Author

Commented:
JonNorman

the code does not work. basically no validation is triggering
Jon NormanInformation Systems Manager
CERTIFIED EXPERT
Top Expert 2012

Commented:
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.
erikTsomikSystem Architect, CF programmer

Author

Commented:
I am using the method but I am  calling the method like this
rules: {
    fileName: "true"
  }
Jon NormanInformation Systems Manager
CERTIFIED EXPERT
Top Expert 2012

Commented:
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.
erikTsomikSystem Architect, CF programmer

Author

Commented:
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 NormanInformation Systems Manager
CERTIFIED EXPERT
Top Expert 2012

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.