0

I'm using the jQuery Validation Plugin on my form.It has an 'Username' field . User can give either email or phoneno as username . How can I validate this field ? I tried this code :

<form id="login">
<input id="username" name="username" />
<input type="submit" value="submit" name="submit" />
</form>

<script>

$("#login").validate(

    {
  rules: {
   username: {
        required: true,
        number: true,
    email:true
              }
           }
         });

madhur
  • 973
  • 1
  • 14
  • 23
  • 1
    Write regular expressions for the two types of input, and combine them with `|`: `/emailregexp|phonenoregexp/` – Barmar Dec 21 '13 at 22:05
  • _"Questions asking for code must demonstrate a minimal understanding of the problem being solved. **Include attempted solutions, why they didn't work, and the expected results**. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)._" – Sparky Dec 22 '13 at 00:06
  • do you mean writing two input tags and comibining jquery for their ids with "|" my preent code is
    – madhur Dec 22 '13 at 00:21
  • Your question is three short sentences. At least put forth as much effort into writing your question as somebody else would have to put forth answering it for you. – Sparky Dec 22 '13 at 00:21
  • @Barmar means that you'll have to write a custom rule using regex. See [the `addMethod` method](http://jqueryvalidation.org/jQuery.validator.addMethod/) of the plugin. Also look at [the source code of the plugin](http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js) to see how regex is used within the methods. – Sparky Dec 22 '13 at 00:21
  • @Sparky is right. jquery-validate doesn't have any built-in multi-type validation, so you have to write your own method. – Barmar Dec 22 '13 at 00:23
  • Do not put chunks of code into comments! Just edit your question to make it better. – Sparky Dec 22 '13 at 00:26
  • Thanks Barmar and Sparky for help, my problem is fixed by using addmethod – madhur Dec 22 '13 at 13:56
  • Please post your detailed answer below for the benefit of others. – Sparky Dec 22 '13 at 15:17
  • I used the addmethod given in the question (http://stackoverflow.com/questions/8393662/jquery-validation-require-either-valid-phone-or-valid-email), (same as stated by @Barmar) added this method to additional-methods.js file of plugin and used this in input as given example in link, it works well. – madhur Dec 22 '13 at 20:46

0 Answers0