Tag Archives: validation

Change Default MVC5 Password Complexity Requirements – Passwords must have at least one non letter or digit character. Passwords must have at least one digit (‘0’-‘9’). Passwords must have at least one uppercase (‘A’-‘Z’)

Hey everyone,

I’ve started on a new MVC5 project and came across the following error message while trying to register a new user:

Passwords must have at least one non letter or digit character.
Passwords must have at least one digit (‘0’-‘9’).
Passwords must have at least one uppercase (‘A’-‘Z’)

While having a secure password is obviously important, I felt that most users would probably find these requirements a little extreme. After a bit of Googling I came across a StackOverflow post that mentioned a config class that you can use to edit these settings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// App_Start > IdentityConfig.cs
 
...
 
// Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };
 
...
Detecting File Size with jQuery

Detecting FileSize – jQuery

Hey everyone,

Just a quick post on how to detect a file’s size with jQuery. I’m currently using a version of this for basic client side validation:

<form action="upload" enctype="multipart/form-data" method="post">
 
    Upload image:
    <input id="image-file" type="file" name="file" />
</form>
$(document).ready(function(){
    $('#image-file').bind('change', function() {
 
        var fileSize = this.files && this.files.length > 0 && this.files[0].size ? this.files[0].size / 1024 / 1024 : 0;
 
        if(fileSize > 0){
            $('body').append('<div class="file valid">' + fileSize + ' MB</div>');  
        }
        else{
            $('body').append('<div class="file invalid">' + fileSize + ' MB</div>'); 
        }            
    });
});
.file{
padding: 5px 7px;
border-radius: 3px;
background-color: green;
color: white;
margin: 5px;
}
 
.file.valid{
background-color: rgb(127, 197, 127);
}
 
.file.invalid{
background-color: rgb(197, 127, 127);
}

Here’s the link to the full fiddle: http://jsfiddle.net/nLLMF/

There’re also a whole heap of other options listed on this StackOverflow post if you’re chasing something a bit more robust: http://stackoverflow.com/a/13249924/522859

preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash – CakePHP

Hey everyone,

Ran into the following error while I was mucking around with CakePHP today:


preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

The error seemed a little weird as I wasn’t using any regex. It turned out that it was because of my custom validation methods. I had marked them as private instead of public. A bit of an ambiguous error message so hopefully this will be able to help some of you out!

Validating Boolean Value of False in Rails

I came across a bit of a weird one this afternoon when trying to validate. Several of my fields appeared to be incorrectly flagged as not having a value.

This is the validation I was using:

#Define validation
validates :order_id, :user_id, :acknowledged, :completed, :presence => true

 

The fields that were flagging as not present were :acknowledged and completed. After hardcoding the values and still not getting anywhere I realised that while values were being provided, they were boolean values of false. Apparently :presence will treat a value of false as not being present.

After another Google search I came across this post which shows how to validate a boolean value.

When implemented within my app, the new validation looks like this:

#Define validation
validates :order_id, :user_id, :presence => true
validates :acknowledged, :completed, :inclusion => {:in => [true, false]}

Hopefully that’ll be able to help a few of you out as well.

Validation Error Messages not Displaying – Ruby on Rails

Just another quick problem I ran into, the validation was indicating that an error had occurred however it did not show what the error was:

1 error prohibited this message from being saved:

— Usually an error would appear here —

Turns out the problem was pretty straight forward – it’s always the little things! I’d simply missed the each when displaying the error messages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%= form_for @location do |f| %>
  <% if @location.errors.any? %>
    <div id="error_explanation">
       <h2><%= pluralize(@location.errors.count, "error") %> prohibited this message from being saved:</h2>
       <ul>
         <% @location.errors.full_messages do |msg| %>
         <% #Should have been: @location.errors.full_messages.each do |msg| %>
         <li>
           <%= msg %>
         </li>
         <% end %>
       </ul>
    </div>
  <% end %>

Simply adding the correct code fixed the problem straight away.

1 error prohibited this message from being saved:

Country can’t be blank

Good Luck!