1

I would like to validate my textbox which accepts Academic Year as input in the format YYYY-YY. How do I write a regular expression validation/ jquery client validation function for this?

function parseDate() {
    alert("yeah");
    var sAcadYearName = document
            .getElementById('<%= txtAcademicYearName.ClientID%>');
    var m = sAcadYearName.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
    return (m) ? new Date(m[3], m[2] - 1, m[1]) : null;
}
8
  • are you using any validation plugin? Commented May 15, 2013 at 5:00
  • 1
    In that case you need to go for an regex based validation Commented May 15, 2013 at 5:02
  • Yeah I did that, but the expression /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ doesn't work ... Commented May 15, 2013 at 5:11
  • can you share the code what you have done Commented May 15, 2013 at 5:12
  • function parseDate() { alert("yeah"); var sAcadYearName = document.getElementById('<%= txtAcademicYearName.ClientID%>'); var m = sAcadYearName.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/); return (m) ? new Date(m[3], m[2] - 1, m[1]) : null; } Commented May 15, 2013 at 5:15

7 Answers 7

3

It depends how complicated you want to go, if you just want to validate numbers it can be as simple as

\d\d\d\d-\d\d

if you want to be just within 19XX-YY or 20XX-YY then

(\b19|\b20)\d\d-\d\d

if you need more specific rules (e.g. 1981-99) and you need to write some custom javascripts to perform validation.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this code

 <input id="txtdob" type="text"/>

 <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ForeColor="Red"
                        ControlToValidate="txtdob" ValidationGroup="check" ErrorMessage="Date format in YYYY-YY"
                        ValidationExpression="^([0-9]{4})[.--]+([0-9]{2})$"></asp:RegularExpressionValidator>

Eidt:

Just change and try it

<input id="txtdob" runat="server" type="text" />

            <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ForeColor="Red"
                ControlToValidate="txtdob" ValidationGroup="check" ErrorMessage="Date format in YYYY-YY"
                ValidationExpression="^([0-9]{4})[./-]+([0-9]{2})$"></asp:RegularExpressionValidator>
            <asp:Button runat="server" ValidationGroup="check" />
        </div>

Or add this line where your code!

var m = sAcadYearName.match(^([0-9]{4})[./-]+([0-9]{2})$);

It's like validate for :

2013/14 or 2013-14

2 Comments

It's Client side validation ! if you use this code,page not refreshing. See my Edited Code
It's okay . Happy Coding !
0

You can create a custom date validator in jQuery

SEE HERE

Comments

0
function parseDate(str) {
  var m = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
  return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}

Comments

0

Check out this jQuery validation plugin. It has demos, and a lot of ways to filter form field input. Enjoy!

Comments

0

You can go this link here is a good example http://www.regular-expressions.info/dates.html

or you can check out this link also

RegEx to match M/YYYY, MM/YYYY , M/YY or MM/YY format

Comments

0

Here is my solution, I have added a regular ecpression validator for my textbox like so.

<asp:RegularExpressionValidator id="txtAcademicYearName_RegularExpressionValidator2"  runat="server" ControlToValidate="txtAcademicYearName" ErrorMessage="Enter year in format  YYYY-YY" ValidationExpression="(\b19|\b20)\d\d-\d\d" ForeColor="Red" Display="None"  ValidationGroup="AcademicYear"></asp:RegularExpressionValidator>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.