
Originally Posted by
trollster
1) The username can be only alphanumeric but must start with an alphabet. No special characters to be allowed.
PHP Code:
// this forces username to be at least 2 chars long and at most 30 chars:
// 1 letter plus 1 to 29 alphanumeric chars.
// ^ means start of the string
// $ means end of the string
// i means case insensitive
if (!preg_match('/^[a-z][0-9a-z]{1,29}$/i', $username)) {
// wrong username!
// ...
}

Originally Posted by
trollster
2) The first name and last name can have only alphabets and not anything else.
PHP Code:
// this forces firstname to be between 2 and 30 chars, only letters
if (!preg_match('/^[a-z]{2,30}$/i', $firstname)) {
// wrong firstname!
// ...
}

Originally Posted by
trollster
3) The roll number can only be a 10 digit number, not anything less, not anything more, no other characters allowed.
PHP Code:
if (!preg_match('/^[0-9]{10}$/', $rollnumber)) {
// wrong roll number!
// ...
}

Originally Posted by
trollster
4) The most important of all, I want users to sign up with email id given by my college. That is, allow registrations only if specific domain exists in the email ID.
PHP Code:
// . has a special meaning in regex so we need to escape it with \
// note that this will not fully validate the email, this will only check if it
// ends with the right domain
if (!preg_match('/@mycollegedomain\.edu$/i', $email)) {
// wrong domain in email!
// ...
}
Bookmarks