Validation of membership registration form

Let's write the validation process of the membership registration form. Validation is the process of verifying that the data entered by the user is correct.

Get the sent user information

The sent user information is obtained from the parameters. Use the param method to get the parameters that contain the user information.

Let's receive the POSTed information from Membership Registration Form. In this process, it is confirmed in advance whether it is POST and it is a temporary registration process.

<%
  #Processing a POST request
  if ($self->methodeq'POST') {
    # Processing method
    my $op = param ('op');
    $op ='' unless defined $op;
    
    # Temporary registration processing
    if ($op eq'register') {
      #Get user information
      # USER ID
      my $code = param ('code');
      
      # password
      my $password = param ('password');
      
      # email address
      my $mail = param ('mail');
    }
  }
%>

User ID validation

Make sure your user ID is correct. The correct user ID is defined by the Mojolicious startup as follows:

  • User ID specified
  • The user ID must consist of at least one ASCII code alphanumerical and an underscore
  • The length of the user ID is within the limit of the length of the user ID that can be used by the service
  • The user ID does not overlap with the user ID of the existing user
  • The user ID does not overlap with the user ID of a user waiting for user authentication with a different email address

Note that the user ID must be unique, so you need to access the database to check this.

Let's write the validation of the user ID. If it is not correct, I try to add it to the error information.

User ID is specified

First of all, make sure your user ID is not empty. Save the errors in a hash reference called $errors. The length of the string can be obtained with the length function.

my $errors = {};
if (length $user_code> 0) {
  
}
else {
  #Error handling
  $errors->{user_code} ='Please specify the user ID. ';
}

User ID consists of one or more ASCII code alphanumers and underscores

Use Perl regular expressions to check that your user ID consists of at least one ASCII code alphanumerical and one underscore character (a-zA-Z0-9_).

Regular expression can be expressed by either of the following two methods. Write it obediently with character class, or use a character class that represents a word character composed of Perl's ASCII code. ..

# Write obediently with a character class
^ [a-zA-Z0-9_] + $

# Use a character class that represents a word character composed of Perl's ASCII code
^ \ p {PosixWord} + $

Let's write it in Perl code.


# Perl word character (a-zA-Z0-9_)
if ($user_code = ~ / ^ \ p {PosixWord} + $/) {
  
}
else {
  #Error handling
  $errors->{user_code} ='The characters that can be used for the user ID are "a-zA-Z0-9_". ';
}

The length of the user ID is within the limit of the length of the user ID that can be used in the service

Check that the user ID length is within the user ID length limit available for the service.

In the service called Twitter, which is used globally, the user ID is limited to 15 characters.

Mojolicious startup will use this as a reference to limit the length of the user ID to 15 characters or less. The length of the string can be obtained with the length function.

#Username length limit 15 characters
if (length $user_code <= 15) {
  
}
else {
  #Error handling
  $errors->{user_code} ='The maximum number of characters that can be used for a user ID is 15 characters. ';
}

(Writing)

Associated Information