jquery file name validation

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);
              }

            },
}\}
LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?
 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
lwadwellCommented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
i did this but still getting an error TypeError: $.validator.methods[method] is undefined
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
erikTsomikSystem Architect, CF programmer Author Commented:
the first time when i click enter the validation does not work , but second it does
0
 
lwadwellCommented:
>> "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
 
lwadwellCommented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
lwadwellCommented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
lwadwellCommented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
good work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.