How to validate these rule in input textfield in JQuery?

Hi there

this is my jsfiddle.net
http://jsfiddle.net/vngx/fqdcq2nf/3/

I want to validate some fields in Input only contain charcters(userFirstName) and some alphanumeric charcters(userPassword) and some field as a specific format(userId)

How to do this?

Thanks

You can add methods to the validator.

The additional-examples.js script is a good example of how that’s done.

For example, the following adds the ability for you to specify a regular expression pattern:

$.validator.addMethod("pattern", function(value, element, param) {
	if (this.optional(element)) {
		return true;
	}
	if (typeof param === "string") {
		param = new RegExp("^(?:" + param + ")$");
	}
	return param.test(value);
}, "Invalid format.");

You would then specify it in your rule like this:

userFirstName: {
    required: true,
    pattern: /^[A-Za-z]{3,}$/
},

That pattern there for example, ensures that there are at least three numbers and letters, underscore and dashes allowed in the first name. I’d think twice about being that narrow though, for some names contain special characters too.

These next methods add hasUppercase and hasLowercase methods to your fields:

$.validator.addMethod("hasUppercase", function(value, element) {
	if (this.optional(element)) {
		return true;
	}
    return /[A-Z]/.test(value);
}, "Must contain uppercase");

$.validator.addMethod("hasLowercase", function(value, element) {
	if (this.optional(element)) {
		return true;
	}
    return /[a-z]/.test(value);
}, "Must contain lowercase");

You can use them like this:

userPassword: {
    required: true,
    hasUppercase: true,
    hasLowercase: true
},

You can use a regular expression for the userid too, with something like this:

userId: {
    required: true,
    pattern: /^\d{2}[a-zA-Z]{2}\d{2}$/
}

@Paul_Wikins

I try to use first rule
but its not working

Your code is inside a function that is not being called from anywhere.

function validationDemo(){

That’s why you can’t get it working.

ok here is my updated code

Its Working now

but its showing “Invalid Format” many times.
My Requirement is
1)if I type any charcter other than A-Z and a-z then it should show error.
2)At any point if I type 2 charcters simultaneously then it is also showinng error messages

How to sort out this?

@Paul_Wilkins

After some how I have managed working of first validation “charcter only”
here is my updated code

but code for uppercase and lower case is not working

The capitalization of your method names is important.

For example:

$.validator.addMethod("hasUppercase", function(value, element) {
...
   userMiddleName:{
        required:true,
        hasUpperCase: true
    },

One of these things is not like the other.

1)What this Syntax mean

/[1]{1,}$/

specially last { }

2)What this line do

return /[A-Z]/.test(value);


  1. A-Za-z ↩︎

That is a regular expression, which is a pattern matching manner in which many useful things can be done.

A good tutorial about them can be found at http://regular-expressions.mobi/tutorial.html

The ^ and $ are for the start and end of the string, which means that whatever matches in between must be the entire string, not just a part of it.

The square brackets are for a group of characters, in this case just uppercase letters.

The curly braces are where can specify a minimum and maximum number of characters that must match. In my earlier example it was a minimum of three, which seems suitable for a username.

That line returns true if there are any upper case characters in the field value.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.