|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Regular Expression form field validation
Hi Everyone,
I need to add a form field validation using Javascript to make sure a text field's value is entered correctly. I lack expertise in this field, so all of this Javascript code is looking like a different language to me... What I need is to validate the field so that it is the following pattern "mmyy" (basically a 4 digit numeric value). I was thinking that function validateValue is the way to go - but I am unsure how to write the rest. Can anyone help me out? |
|
#2
|
|||
|
|||
|
Quote:
Code:
function validater(fld) {
var fourDigits = /^\d{4}$/;
var formValue = document.demo.monthYear.value;
var trimmed = formValue.replace(/^\s+|\s+$/g, '');
if (fourDigits.test(trimmed))
{
alert("Four Digits");
}
else
{
alert("Not four digits.");
}
return false;
}
|
|
#3
|
|||
|
|||
|
Awesome! That is what I needed. Thanks.
|
|
#4
|
|||
|
|||
|
One more question......
Is there anyway to specify that the digits must meet a certain criteria? For example, since this is for an expiration date, I do not want someone to enter a month and date that has already passed. Can this be done in the expression or would I need some additional Javascript coding to validate the field? |
|
#5
|
|||
|
|||
|
need some additional Javascript coding
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|