Need Some Change in RegExp of JQuery Validation

Hi there

this is my jsfiddle

I am validating only character it is working fine
When I type
“Manish” its working fine
but when I type
“Manish Joshi”

it is showing error because of backspace between words
So I want that rule to escape any number of backslash only validate input data
How do I acheive this?

Thanks

add \s to your pattern

pattern: /^[A-Za-z\s]{1,}$/

@megazoid

Thanks for help

Hi
this is my new updated JS fiddle

Now I want to date field in such format
dd/mm/yyyy

default date function of JQuery is not validating in this format

it only validate to
01/01/1

I want validate till
01/01/1991

How do I acheive this?

Although adding \s
is working but I want following format should also accept
“G.I.C.Lohaghat”

it is not accepting this format

\s is to allow spaces, and \. Is to allow fullstops. Add \. To the character group in square brackets.

Quoting from the jQuery Validate page about their date method, from where it seems that you’ve copied your code from:

dateISO is the yyyy/mm/dd format, which may not be suitable, and the localisations are useful if you are wanting messages in Romanian for example, so perhaps not that.

Which leaves - the additional methods script that you should include along with the validate script, where you can use one of the date methods in there.

You’ll find that dateITA is the most suitable one, validating for dd/mm/yyyy dates.

@Paul_Wilkins
Thanks

Now its working full code is here

can you explain a little bit how dateITA is working here?

Sure - I’ll intersperse comments throughout the dataITA code that the additional-methods script provides.

/**
 * Return true, if the value is a valid date, also making this formal check dd/mm/yyyy.
 *
 * @example $.validator.methods.date("01/01/1900")
 * @result true
 *
 * @example $.validator.methods.date("01/13/1990")
 * @result false
 *
 * @example $.validator.methods.date("01.01.1900")
 * @result false
 *
 * @example <input name="pippo" class="{dateITA:true}" />
 * @desc Declares an optional input element whose value must be a valid date.
 *
 * @name $.validator.methods.dateITA
 * @type Boolean
 * @cat Plugins/Validate/Methods
 */

The first line of the comment block gives a good overview of what the below code does. The rest of the comment block gives ideas and details to people using dateITA on other ways that it can be used too.

$.validator.addMethod("dateITA", function(value, element) {
	var check = false,

The check variable will be used to record if we think the date is valid or not. Following the standard validation principle, we believe first that it’s false, and only make it true when we know that it is valid.

		re = /^\d{1,2}\/\d{1,2}\/\d{4}$/,

This regular expression checks that the date is properly formatted, as nn/nn/nnnn with one of two digits allowed in the day and month areas, but that’s all that it checks. 99/99/9999 is also valid according to the regular expression. So, further checks are needed.

		adata, gg, mm, aaaa, xdata;

Those other variables are used throughout the code, and so are declared at at the start here.

	if ( re.test(value)) {

If the regular expression test fails, it’s clearly not a valid date.

		adata = value.split("/");
		gg = parseInt(adata[0], 10);
		mm = parseInt(adata[1], 10);
		aaaa = parseInt(adata[2], 10);

The above splits up te date in to its three parts. gg for the day, mm for the year, and aaaa for the year. Why is the author using strange variable names such as gg and aaaa? Perhaps he’s from a nation or culture that we don’t know of. Using better named variables such as dd and yyyy would work better though, and make the rest of he code more understandable.

		xdata = new Date(aaaa, mm - 1, gg, 12, 0, 0, 0);
		if ( ( xdata.getUTCFullYear() === aaaa ) && ( xdata.getUTCMonth () === mm - 1 ) && ( xdata.getUTCDate() === gg ) ) {

Create a date, and check to see if the date values match up with what was entered.
JavaScript date does allow bad date values to be used. For example, if in JavaScript you enter 29/02/2015 and it’s not a leap year, it rolls over to 01/03/2015. Checking that the values match helps to prevent such problems.

The date method also has an interesting little detail where months are given as being from 0 to 11, so we need to remove one from the provided date for that to work properly.

			check = true;
		} else {
			check = false;
		}

The date is good or bad, depending on if the dates match or not.

	} else {
		check = false;
	}

When the first regular expression test fails, it’s clearly not a valid date either.

	return this.optional(element) || check;

If the date is optional and not required, don’t even bother saying if it’s valid or not.
If it’s not an optional one though and is required, the validated check value is returned.

}, "Please enter a correct date");

This is the error message to provide when the validation check fails.

Thanks
@Paul_Wilkins

one more thing
dateITA only validate date in this format
dd/mm/yyyy

I want to validate in this format
mm/dd/yyyy

suppose I want to validate 28-April-2015
so I am trying to validate
“04/28/2015”

but dateITA not validating this date.

What change I need on this?

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