Link to home
Start Free TrialLog in
Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on

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

            },
}\}
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

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

Avatar of erikTsomik

ASKER

i did this but still getting an error TypeError: $.validator.methods[method] is undefined
the first time when i click enter the validation does not work , but second it does
>> "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.
ASKER CERTIFIED SOLUTION
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.
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
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().
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
good work