#19 ✓not_for_core
Onur Köse

Validator patch

Reported by Onur Köse | April 16th, 2008 @ 06:36 PM

Hi everybody,

Here is the some patches that i use for validating limited strings, passwords or e-mail addresses.

  validate: function(type, lengthMin, lengthMax) {
    switch ( type ) {
      case 'password':
        allowedChars = '!@#$%^&*?_~-1234567890abcçdefghıijklmnoöprsştuüvyzqwxABCDEFGHIİJKLMNOÖPRSŞTUÜVYZQWX';
        break;
      case 'username':
        allowedChars = '_1234567890abcdefghijklmnoprstuvyzqwxABCDEFGHIJKLMNOPRSTUVYZQWX';
        break;
      case 'string':
        allowedChars = '1234567890abcçdefghıijklmnoöprsştuüvyzqwxABCDEFGHIİJKLMNOÖPRSŞTUÜVYZQWX';
        break;
      case 'email':
        allowedChars = '1234567890abcdefghijklmnoprstuvyzqwx[].+@-_ABCDEFGHIJKLMNOPRSTUVYZQWX';
        cf = /(.*)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
        cn = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,8}|[0-9]{1,3})(\]?)$/;
        break;
    }
    
    if( ( this.length < lengthMin  ||  this.length > lengthMax ) && type != 'email' )
      return false;
    else{
      for(var i=0; i<this.length; i++)
        if (allowedChars.indexOf(this.charAt(i))<0)
          return false;

      if ( type == 'email' )
        if(!this.match(cf) && this.match(cn))
          return -1;
      
      return -1;
    }
  }

Explanation: I add this patch to Object.extend(String.prototype, {...}); object -lines 334 to 559 in prototype-1.6.0.2.js-

Using: I use it for validation in forms. For example:

//validating password field, min: 6 max: 20 char
if ( !$F('password').validate('password', 6, 20) ) {
  alert('Passwords must be in 6-20 chars.');
  $('password').focus();
  return false;
}

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Pages