?
Solved

jquery file name validation

Posted on 2012-08-23
12
Medium Priority
?
1,583 Views
Last Modified: 2012-10-14
I ma using jquery validation plugin and I need to validate file name so file name should be for example index.htm and nothing else. I tried using the add method but it does not first time around

$.validator.addMethod("filename", function(value, element) {
            
        return this.optional(element) || /[a-z]\w*\.(html|htm|swf)$/i.test(value);
      }, "Please specify a valid file name");

{
 rules: {
file1: {
                  required: function(element) {
                    return ($("#locOwner").val()== 0 && $("#server").val()== 0);
              },
              filename: function(element){
                    return ($("#userID").val()== 0 && $("#nameID").val()== 0);
              }

            },
}\}
0
Comment
Question by:erikTsomik
  • 7
  • 5
12 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 38328035
when is addMethod() being applied?  On document ready?

My example:
    <script>
        $(document).ready(function(){
            $.validator.addMethod("filename", function(value, element) {
                return this.optional(element) || /[a-z]\w*\.(html|htm|swf)$/i.test(value);
            }, "Please specify a valid file name");
            
            $("#myForm").validate({
                rules: {
                    afile: {required: true, filename: true}
                }
            });
        });
    </script>
</head>
<body>
    <form class="aform" id="myForm" method="post" action="">
        <fieldset>
            <legend>A simple comment form with validation</legend>
            <p>
                <label for="afile">File Name</label>
                <em>*</em>
                <input type="text" id="afile" name="afile" size="60"  class="afile" value="" />
            </p>
            <p>
                <input class="submit" type="submit" value="Submit"/>
            </p>
        </fieldset>
    </form>
</body>

Open in new window

0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38328987
i did this but still getting an error TypeError: $.validator.methods[method] is undefined
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38329017
the first time when i click enter the validation does not work , but second it does
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 25

Expert Comment

by:lwadwell
ID: 38329112
>> "i did this but still getting an error..."
what did you do exactly?  

The code snippet in the question is not complete enough for me to understand fully.
0
 
LVL 19

Accepted Solution

by:
erikTsomik earned 0 total points
ID: 38329276
i tried doing this way but still getting an error message

 $(document).ready(function(){
            $.validator.addMethod("filename", function(value, element) {
                return this.optional(element) || /[a-z]\w*\.(html|htm|swf)$/i.test(value);
            }, "Please specify a valid file name");
           
            $("#myForm").validate({
                rules: {
                    afile: {required: true, filename: true}
                }
            });
        });
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38329335
This is my full test html.  It works for me (in chrome).  Try it 'as is' for yourself and see if you get the error with that file.
Q-27840740.html
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38329378
it does not work for me I still able to submit without proper validation. I mean my code is still not working I tried yours and it does
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38329415
How are you submitting?  Is an Ajax call on submit?  If so, does that ajax routine check for validation errors before the call.
I am just throwing out ideas as I cannot see you full code.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38329434
I submit on submit. Also I've notice that I only get this error when i create a record if I come to edit the same record it validates properly without error messages
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38329465
Oh ... when you "create a record" are you defining it inside a predefined form/inputs or building a new one?  If a new one ... the validate may not have been bound to it as it didn't exist on page ready().
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 38329645
I think i found the problem . ON the page I have a custom popup that opens up in jquery dialog, and i think it overwrites the javasciprt
0
 
LVL 19

Author Closing Comment

by:erikTsomik
ID: 38494375
good work
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question