Jump to content

Recommended Posts

Hi All,

 

In my student login form there are 2 fields namely, birth day and NIC. 

 

I want something like this to be done. When a user enters a NIC such as 85_____, birth date should be like 05-08-1985. Therefore the year of the birth date and the first 2 digits of the NIC number should be same. On the other hand user should not be able to select any difference year as birth date. I want to know how this kind of a validation done by using PHP and javascript?

Link to comment
https://forums.phpfreaks.com/topic/268057-need-ideas-regarding-form-validation/
Share on other sites

Hi All,

 

In my student login form there are 2 fields namely, birth day and NIC. 

 

I want something like this to be done. When a user enters a NIC such as 85_____, birth date should be like 05-08-1985. Therefore the year of the birth date and the first 2 digits of the NIC number should be same. On the other hand user should not be able to select any difference year as birth date. I want to know how this kind of a validation done by using PHP and javascript?

 

How does 85_____ become 05-08-1985?

And why is it not exactly that, just like that?

 

Anyways the later part makes me think you want this:

$nic = '85_____';
echo $nic[0].$nic[1];

essentially use substr to shorten the NIC to the first 2 digits, and again use it to get the last 2 digits of birthday, notice the differnce of the start point, 0 starts at the beginning for NIC and -2 starts 2 from the end of birthday

 

$NIC = '85589458904';
$birthday='05-08-1985';
echo substr($NIC, 0,2);
echo substr($birthday, -2,2);
if(substr($NIC, 0,2) != substr($birthday, -2,2)) echo "no match";

 

this is a server side colution, but apply the same principles into javascript for a client side solution

Thanks a lot for your reply.. :)

 

Can this be extended for the following scenario as well? I wonder is it possible..

 

Suppose there are classes from grade 1 - 13. When registering students admin supposed to enter birth date and relevant class. A student should be at least 6 years old to admit grade 1 . I have created the following table based on year 2012 as the current year.

 

 

Birth Date                      Student Class

 

2006-08-22                            1

2005-11-25                            2

----------------------------------------

----------------------------------------

94-05-06                              12

93-02-02                              13

 

of course it can be done, but perhaps with this new snippet i gave you, you could attempt something yourself first and come back when you get stuck. you would need to make a different comparison, my initial thoughts are a switch statement for the different scenarios rather than a very long  if/elseif statement

There are some things that doesn't really make too much sense...

1993: 13

1994: 12

1995: 11

1996: 10

1997: 9

1998: 8

1999: 7

2000: 6

2001: 5

2002: 4

2003: 3

2004: 2

2005: 1

2006: 0

 

So, it is not enough for them to become 6 the year they start going to school? They need to be 6 when they start going to school?

 

If that is the case, you will just have to compare the month first and check if it's above a certain number, and then do the same with the day. If I've understood this correctly, these numbers has moved two spots to the right after when we got to year 2000, so you will have to know the year to find the month and day. It should be pretty easy to implement with this code, just ask if you need more help:

$nic = '9306____';
$oldest_year = 93; // write 20xx as 1xx
$oldest_class = 13;
$year = substr($nic, 0, 4);
if($year[0]==2 && $year[1]==0){
$year -= 1900;
}else{
$year = substr($nic, 0, 2);
}
$class = $year-((($year-$oldest_year)*2)+($oldest_year-$oldest_class));

This code is only looking at the age, and 2006 would be 0, while 93 = 13.

 

I haven't put too much thought into the math, probably possible to write it cleaner. Might be easier to use if you edit it so you use youngest year and class instead.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.