-
Strings in javascript
Hi,
I need some examples of string functions in javascript. My aim is to validate a date value to check that it is in the format that I want it to be in. I will need to an equivilent of vb split function or instr and mid.
Anyone got some examples for me?
-
there is a javascript split function too. Theres also substring and indexOf that maybe what you require.
Have a read of this. It shows all methods and functions associated with the js string object
http://www.w3schools.com/js/js_string.asp
-
You can also check my sig for links to the documentation on JavaScript, where you can find everything you need to know about the String object.
-
You can use regular expressions for patter matching
eg.
Code:
//lets check for dd/mm/yyyy
correct_date = "02/03/2002";
incorrect_date = "02/03/02";
if( correct_date.match(/^\d{2}\/\d{2}\/\d{4}$/))
{
alert("date matches");
}
if( incorrect_date.match(/^\d{2}\/\d{2}\/\d{4}$/))
{
alert("date matches");
}
the previous example uses this regular expression.
^\d{2}\/\d{2}\/\d{4}$
and the regular expression is enclosed by /regex/
^ = match the begiing of the string
\d = any numeric char
{n} = match n times the previous element
\/ = a literal /
$ = match the end of the string