Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I want to validate my string for month and year like "June 2016" can any one help me to solve this.

What I have tried:

if (!$("#txtmonthyear").val().trim().match("^[0-9 ]*[a-zA-Z ]+[a-zA-Z0-9 ]*$")) {
return false;
}
Posted
Updated 21-Oct-16 3:24am
Comments
Richard MacCutchan 20-Oct-16 7:49am    
Look at your source data:
1. Starts with upper case [A-Z] (or maybe only the letters of month names)
2. followed by up to 8 lower case [a-z]
3. followed by space
4. followed by four digits, the first one not being 0

Your sample above does not look like it matches.

I regularly use the Regular Expression Library[^] to help find and validate REGEX.

That said, I think it's a good thing to learn how these things work for yourself. MS has some really good references for using REGEX with C#:
Regular Expression Language - Quick Reference[^]

In your case, you want a month and year. We could use the regex to extract them from a string and use a post-submit validator, but it looks like you want a pre-submit so we'll keep it easy.

C#
"^[a-zA-Z]+\s{1}[0-9]{4}$"


This simple validator will simply evaluate that a word is followed by a space is followed by a 4-digit number. Here's the breakdown:
^ Start evalation at the first character of the string.
[a-zA-Z]+ match alphabetic characters, lower and upper case, one or more times.
\s{1} match whitespace exactly once.
[0-9]{4} match a numeric digit exactly 4 times
$ match the string terminator

You can do a hell of a lot more with REGEX should you so choose. It's a superbly useful tool that gets ignored too often due to the oblique syntax.
 
Share this answer
 
Try this:
[ADFJMNOS][a-z]+ [12][0-9][0-9][0-9]\b

which can no doubt be improved upon.

[edit]
Such as:
[ADFJMNOS][a-z]{2,8}\s[12][0-9]{3}\b

[/edit]
 
Share this answer
 
v3
Comments
jaideepsinh 20-Oct-16 8:16am    
I user like this:
if (!$("#txtmonthyear").val().trim().match("^[ADFJMNOS][a-z]+ [12][0-9][0-9][0-9]*$")) {
return false;
}
but not working
Richard MacCutchan 20-Oct-16 8:22am    
I tested the above and it worked for two different dates. You need to check exactly what string you are trying to match against.
jaideepsinh 20-Oct-16 9:19am    
it is not check last four character is number or not it is only check last character is number.
Richard MacCutchan 20-Oct-16 9:32am    
No it does not, you have added an asterisk after the last [0-9]. And you still have not explained what string you are trying to match against.
jaideepsinh 21-Oct-16 3:39am    
i am validate this "Ja 20000" and this both not working in my c# code
Not a solution, but tools, I recommend last one to debug a RegEx:

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
If you insist on regex, than try this:
January|February|June\s\d{4}

You will have to fill in the rest of the month names.
However, it is neither efficient nor fool proof to use regex to validate a date as it has far too many formats.
Instead, try DateTime.TryParseExact Method (System)[^]. See example below:
C#
using System;
using System.Globalization;
					
public class Program
{
	public static void Main()
	{
		string input = "June 2016";
		string format = "MMMM yyyy";
		DateTime dateTime;
		if (DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
		{
	    	//Console.WriteLine(dateTime);
			Console.Write("valid");
		} else {
			Console.Write("Not Valid");
		}		
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900