Krajee

Yii2 Masked Input

Thankful to Krajee! BUY A COFFEEor to get more out of us.

Demonstration and usage examples for the Yii 2.0 Masked Input widget. Widget revamped with upgraded plugin that addresses Issue # 3196.

Basic Mask: (999) 999-9999 format
  1. echo MaskedInput::widget([
  2. 'name' => 'input-1',
  3. 'mask' => '(999) 999-9999'
  4. ]);
Mask with dynamic syntax: 9-a{1,3}9{1,3} format
  1. echo MaskedInput::widget([
  2. 'name' => 'input-2',
  3. 'mask' => '9-a{1,3}9{1,3}'
  4. ]);
Non Greedy Repeat: ~ mask "9" or mask "99" or ... mask "9999999999"
  1. echo MaskedInput::widget([
  2. 'name' => 'input-3',
  3. 'mask' => '9',
  4. 'clientOptions' => ['repeat' => 10, 'greedy' => false]
  5. ]);
Optional Input: Using optional inputs like in IP addresses (refer a better IP validation below)
  1. echo MaskedInput::widget([
  2. 'name' => 'input-4',
  3. 'mask' => '9[9][9].9[9][9].9[9][9].9[9][9]'
  4. ]);
Multiple Masks. Notice how the formats change automatically based on the number of characters typed
  1. echo MaskedInput::widget([
  2. 'name' => 'input-5',
  3. 'mask' => ['99-999-9999', '999-999-9999']
  4. ]);
License plate format
  1. echo MaskedInput::widget([
  2. 'name' => 'input-6',
  3. 'mask' => '[9-]AAA-999'
  4. ]);
Using Extensions: Date input (dd/mm/yyyy) format
  1. echo MaskedInput::widget([
  2. 'name' => 'input-31',
  3. 'clientOptions' => ['alias' => 'date']
  4. ]);
Using Extensions: Date input (mm/dd/yyyy) format
  1. echo MaskedInput::widget([
  2. 'name' => 'input-32',
  3. 'clientOptions' => ['alias' => 'mm/dd/yyyy']
  4. ]);
Using Extensions: Numeric input (decimal) format. Group separator: , and Radix point: ..
  1. echo MaskedInput::widget([
  2. 'name' => 'input-33',
  3. 'clientOptions' => [
  4. 'alias' => 'decimal',
  5. 'groupSeparator' => ',',
  6. 'autoGroup' => true
  7. ],
  8. ]);
Using Extensions: IP Address format.
  1. echo MaskedInput::widget([
  2. 'name' => 'input-34',
  3. 'clientOptions' => [
  4. 'alias' => 'ip'
  5. ],
  6. ]);
Using Extensions: URL format. Type "www.yoursite.com".
  1. echo MaskedInput::widget([
  2. 'name' => 'input-35',
  3. 'clientOptions' => [
  4. 'alias' => 'url',
  5. ],
  6. ]);
Using Extensions: Email Address format.
  1. echo MaskedInput::widget([
  2. 'name' => 'input-36',
  3. 'clientOptions' => [
  4. 'alias' => 'email'
  5. ],
  6. ]);
Custom Definitions: Define your own mask definitions (Basic Year).
  1. echo MaskedInput::widget([
  2. 'name' => 'input-37',
  3. 'mask' => 'j', // basic year
  4. 'definitions' => ['j' => [
  5. 'validator' => '[0-9\(\)\.\+/ ]',
  6. 'cardinality' => 4,
  7. 'prevalidator' => [
  8. ['validator' => '[12]', 'cardinality' => 1],
  9. ['validator' => '(19|20)', 'cardinality' => 2],
  10. ['validator' => '(19|20)\\d', 'cardinality' => 3],
  11. ]
  12. ]]
  13. ]);
Custom Definitions: Define your own mask definitions (Advanced Year).
  1. $script = <<< SCRIPT
  2. function (chrs, buffer, pos, strict, opts) {
  3. var valExp2 = new RegExp("2[0-5]|[01][0-9]");
  4. return valExp2.test(buffer[pos - 1] + chrs);
  5. }
  6. SCRIPT;
  7. echo MaskedInput::widget([
  8. 'name' => 'input-38',
  9. 'mask' => 'y',
  10. 'definitions' => ['y' => [
  11. 'validator' => new \yii\web\JsExpression($script),
  12. 'cardinality' => 2,
  13. 'definitionSymbol' => 'i'
  14. ]]
  15. ]);

Note

You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.

The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.

 
4,688,032 visitors to Krajee Yii2 Demos since 22-May-2017