0

I'm using the jQuery form validation script here: http://jzaefferer.github.com/jquery-validation/jquery.validate.js

Most of the methods I need (require field, maxlength, etc.) were already bundled into that script but there are two particularly crucial ones that I think I'll need to code myself.

  1. One of the fields is for image submission. First, I need to to check that the image being submitted is actually linking directly do an image.

  2. Further, I need it to confirm that the image being submitted is NOT is GIF image.

  3. And lastly, (this may be tough) I need it to go into my MySQL database of images and confirm that that image has not been submitted previously (under the same url)

Please help me out if you can point me in the direction of making at least one of these happen! Thanks!

EDIT so this is useful to others who run into this problem:

The first one was solved by adding a method for verifying it was an image: Used code help from Dave and addMethod suggestion from Innerpeacer. Also look at bshack's answer from this question. Also, regex for images:

^(http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(?:\/\S*)?(?:[a-zA-Z0-9_])+\.(?:jpg|jpeg|gif|png))$

Now on to the second part.

Community
  • 1
  • 1
Mark Lyons
  • 1,354
  • 6
  • 21
  • 57
  • With $('input[type=file]').val() you get the filename. You can do some string validations to check the extension for a GIF file. – David Morabito Dec 08 '11 at 03:28
  • I'm trying to figure out a way to put this into the validate.js. For example, here's what they have for ensuring that the input given was in digits: `digits: function(value, element) { return this.optional(element) || /^\d+$/.test(value); },` Any ideas on how to incorporate the image into this? – Mark Lyons Dec 08 '11 at 03:42
  • I don't know how your library works. If it is build on Jquery UI plugins, it's not difficult to add a new feature. Do you want to add some code or the URL of the library? EDIT: Create another method, get the value and trim the last 3 chars. Validate if it is a gif file. – David Morabito Dec 08 '11 at 03:44
  • I think I linked to the library in the post, the github link. I don't understand jQuery all that well yet, but I can kinda see that they're changing up a bunch of things so that they can put all the different methods together. An example of one was the digits code I showed you. What I want to do is add a new method for checking if the file is an image and then one for confirming that it's not a gif. – Mark Lyons Dec 08 '11 at 03:49

2 Answers2

1

Use addMethod. http://docs.jquery.com/Plugins/Validation/Validator/addMethod

However, if you really need to verify that the file is not GIF, nor has it been uploaded to your database, I think you would have to do it in server side?

Innerpeacer
  • 1,321
  • 12
  • 20
  • I can probably do the database checking server side (form action) if it isn't possible. I was thinking I could use some kind of AJAX magic maybe. – Mark Lyons Dec 08 '11 at 04:03
  • I kind of understand the addMethod and how I would make the one I'm looking for, but I don't really know where I should put it. In the script I have in the file where the form resides or the actual validate.js file? – Mark Lyons Dec 08 '11 at 04:15
  • I don't want to select any one answer as you and Dave both helped immensely, upvoted though, thanks! – Mark Lyons Dec 08 '11 at 05:16
  • I think addMethod() can just be put anywhere before you call YouForm.validate(). – Innerpeacer Dec 08 '11 at 06:29
1

I wanted to write this in a regular comment, but it has a lot of code:

digits : function(value, element){ ... }, // comma here
image : function(value, element){
    return this.optional(element) || YOUR_REGULAR_EXP.test(value);
}, // comma here

Just need to find a regular expression to match your criteria, maybe something like:

@"(.*?)\.(jpg|jpeg|png|gif)$
David Morabito
  • 1,498
  • 1
  • 12
  • 22
  • I added that code in the validate.js and tried an image expression. I also just went through the code and found out what else they added to make the digits method work and added one for the image (where applicable) but now the form isn't validating anything. I'll look through it, but if you have any ideas, please let me know. Thanks so far! – Mark Lyons Dec 08 '11 at 04:36
  • I don't want to select any one answer as you and Innerpeacer both helped immensely, upvoted though, thanks! – Mark Lyons Dec 08 '11 at 05:16