0

i write a routine which validate input. it is working fine but when i enter character a then it just accept. here is my code.

function isValidPhoneNumber(val) {
        var flag = true;
        var invalidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@!$#";
        if (flag) {
            for (index = 0; index <= val.length - 1; index++) {
                var phchars = val.substring(index, index + 1);
                if (invalidChars.indexOf(phchars) > 0) {
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }

suppose if i gave val='+9122a5669974' then it return true. indexof function not being able to find the a....why. please tell me what is wrong in the routine. if any char found which exist in invalidChars variable then routine should return false but when a found then routine return true. help me to catch the error. thanks

2
  • 3
    use regular expressions vor this kind of exercises Commented Jun 28, 2013 at 9:43
  • For info on regular expressions in JavaScript you can check out here and here. You can test and have a fiddle with them on RegExr and w3schools has a short set of examples showing you how to use them. Commented Jun 28, 2013 at 9:57

3 Answers 3

2

Your line

if (invalidChars.indexOf(phchars) > 0) {

should be

if (invalidChars.indexOf(phchars) > -1) {

however, I would look into using a regular expression as a better approach.

Sign up to request clarification or add additional context in comments.

Comments

2

Please try with below code line.

if (invalidChars.indexOf(phchars) != -1) {

Comments

1

try this way,

jQuery.validator.addMethod("mobileNL", function(value, element) {
return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-  \s]?)?|0)6((\s|\s?\-\s?)?[0-9]){8}$/.test(value);
}, "Please specify a valid mobile number");

Mentioned method available in additional-methods.js

It's fun using jquery validation plugin for client side validation. Importantly you can add your custom method to it.

  1. Download & Add following script to master, layout or wherever you want to enable client side validation.

    1.1 jquery-1.8.3.min.js(or above) Link: http://code.jquery.com/jquery-1.8.3.min.js

    1.2 jquery.validate.min.js & additional-methods.js (http://jqueryvalidation.org/)

2.Now create your validation rule & message. For example

var jq = jQuery.noConflict(true);
(function ($) {
$(document).ready(function () {

    $('#FormName').validate({
        highlight: function (element, errorClass, validClass) {
            $(element.form).find("input[id=" + element.id + "]").addClass("input");

        },
        rules: {
            PhoneNumber: {
                required: false,
                mobileNL: true,
                max: 10
            }

        },
        messages: {
            PhoneNumber: {
                mobileNL: "Please enter a correct phone",
                 max: "Please specify the number less or equal to 10"
            }

        },
        submitHandler: function (form) {
            form.submit();
            return false;
        }
    });
});
 })(jq);

Create a new js file, saved as jq_validation.js add the above code. And call it in this order

  1. Your html should be like this and done.

    "<%: Html.TextBoxFor(model => model.PhoneNumber, new { @class = "input" })%>"

4 Comments

how to call this method? can u show. how the above regex works...what kind of char it would accept. thanks
You can change regex as per your phone number format. Or let me know your phone number format, may help you to write regex.
i never use jquery validator for client side validation. can u guide me how to use it. is there any good tutorial available for beginning with jquery validator.thanks for your effort.
ya sure, just follow the above steps. I have tried to mention every possible information to implementation jquery validation. Please let me know, specifically which you didn't understand or need guidance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.