473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Regex.match returns null

112 100+
hi there

it seems to be simple but i do not know why my java script function does not work.
Expand|Select|Wrap|Line Numbers
  1. function validatePartialDateInput(SourceField, FieldLabel) {
  2.  
  3.         var sField = document.getElementById('ctl00_ContentPlaceHolder1_'+ SourceField);
  4.  
  5.         var regex_dt = '/^\d{1,2}[/]\d{4}$/';
  6.         alert((sField.value));
  7.         alert(regex_dt.match(sField.value));//returns null
  8.         if(sField.value.length > 0)
  9.         { 
  10.         if (regex_dt.match(sField.value))
  11.         { 
  12.         alert(sField.value);
  13.             alert(FieldLabel);
  14.         }
  15.         }
  16.     }
the regex.match returns null....I have no clue why....can somebody please throw some light on this..thank you
Dec 13 '11 #1

✓ answered by Rabbit

If you use RegExp to create your regular expression, you don't use the regular expression delineators. And if you're using RegExp, you need to escape all forward slashes.
var regex_dt = new RegExp('(0[1-9]|1[012])[- /.](19|20)\\d\\d', "");

8 8083
Rabbit
12,516 Expert Mod 8TB
You haven't created a regex object. All you have there is a string.
Dec 13 '11 #2
user1980
112 100+
thank you..i thought if it is java script I don't have to create a object.
anyways, when I created the object it worked...
Expand|Select|Wrap|Line Numbers
  1.     var regex_dt = new RegExp('/^\d{1,2}[/]\d{1,4}$/',"");

however, my regex always returns false..
Expand|Select|Wrap|Line Numbers
  1. function validatePartialDateInput(SourceField, FieldLabel) {
  2.  
  3.         var sField = document.getElementById('ctl00_ContentPlaceHolder1_'+ SourceField);
  4.  
  5.         var regex_dt = new RegExp('/^(0[1-9]|1[012])[- /.](19|20)\d\d$/',"");
  6.         alert((sField.value));
  7.         alert(regex_dt.test(sField.value));//always returns false
  8.         if(sField.value.length > 0)
  9.         { 
  10.         if (!regex_dt.test(sField.value))
  11.         { 
  12.             alert(FieldLabel);
  13.         }
  14.         }
  15.     }
i have changed my regex to , '/^(0[1-9]|1[012])[- /.](19|20)\d\d$/' to validate mm/yyyy. it always returns false....thank you
Dec 13 '11 #3
Rabbit
12,516 Expert Mod 8TB
If you use RegExp to create your regular expression, you don't use the regular expression delineators. And if you're using RegExp, you need to escape all forward slashes.
var regex_dt = new RegExp('(0[1-9]|1[012])[- /.](19|20)\\d\\d', "");
Dec 13 '11 #4
user1980
112 100+
thank you for the reply but still no use....
Dec 14 '11 #5
Rabbit
12,516 Expert Mod 8TB
I missed the closing quote on the regex string. Put that back in and it should work.
Dec 14 '11 #6
user1980
112 100+
I do have the closing quote...anyways, this is what i have now...
Expand|Select|Wrap|Line Numbers
  1. function validatePartialDateInput(SourceField, FieldLabel) {
  2.  
  3.         var sField = document.getElementById('ctl00_ContentPlaceHolder1_'+ SourceField);
  4.  
  5.         var regex_dt = new RegExp('^(0[1-9]|1[012])[- /.](19|20)\d\d$',"");
  6.         alert((sField.value));
  7.         alert(regex_dt.test(sField.value));
  8.         if(sField.value.length > 0)
  9.         { 
  10.         if (!regex_dt.test(sField.value))
  11.         { 
  12.            alert(FieldLabel);
  13.         }
  14.         }
  15.     }
Dec 14 '11 #7
Rabbit
12,516 Expert Mod 8TB
That's not even what I have in my code. Look again at my post.
Dec 14 '11 #8
user1980
112 100+
I have not taken a closer look at the expression..now it works..thank you....
Dec 14 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rene van Hoek | last post by:
Hi, I am using Xalan 1.8.0 and Xerces 2.6.0 in C++. I have an XML document which I first transform into an other XML document using an XSL styelsheet. Then I want to parse with XPathEvaluator...
0
by: Lee Kuhn | last post by:
Is there a way to add a literal character to a regex match? For example, normally applying the regex ..$ to the string "123456" results in a match "56". I want to be to add a literal character to...
3
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples...
1
by: DKode | last post by:
I have the following regex that I am using: Match match = Regex.Match(global, "%(?<column>.+?)%"); my input string (global) is this: "something\%username%\somethingelse\%match2%\some\%match3%...
1
by: tdmailbox | last post by:
I have the following regular expression. It works fine if the regex code returns a match. However if not the .match code fails. How can I code this so that it skips the match if the regular...
2
by: Beowulf | last post by:
If I run this statement in Query Analyzer, it properly returns 1 for my testing table. But if I put the statement into a stored procedure, the stored procedure returns NULL. What am I doing...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
3
by: Hrvoje Niksic | last post by:
I often have the need to match multiple regexes against a single string, typically a line of input, like this: if (matchobj = re1.match(line)): ... re1 matched; do something with matchobj ......
1
by: Maurizio Vitale | last post by:
Has to be something really stupid, but the following never finish (running Python 2.5.1 (r251:54863, Jan 10 2008, 18:00:49) on linux2). The intention is to match C++ identifiers, with or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.