Menus

Wednesday, 6 September 2017

Yii2 Masked Input


Class yii\widgets\MaskedInput



MaskedInput generates a masked text input.
MaskedInput is similar to yii\helpers\Html::textInput() except that an input mask will be used to force users to enter properly formatted data, such as phone numbers, social security numbers.

Yii2 MaskedInput to collect phone numbers:

echo MaskedInput::widget([
    'name' => 'phone',
    'mask' => '999-999-9999',
]);
You can also use this widget in an yii\widgets\ActiveForm using the widget() method, for example like this:
<?= $form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::className(), [
    'mask' => '999-999-9999',
]) ?>

Demonstration and usage examples for the Yii 2.0 Masked Input widget


Masked input to collect date from users


More Masked Inputs

Code paste to your view
use yii\widgets\MaskedInput;


  1. echo MaskedInput::widget([
  2. 'name' => 'input-31',
  3. 'clientOptions' => ['alias' => 'date']
  4. ]);




Example

Masked date input use in active form.


<?= $form->field($model, 'date_of_birth')->widget(\yii\widgets\MaskedInput::className(), [
        'name' => 'input-31',
        'clientOptions' => ['alias' =>  'dd-mm-yyyy'],
        
]) ?>



Retype your date format in  'clientOptions' => ['alias' =>  'dd-mm-yyyy']


Masked  Amount input 

  1. echo MaskedInput::widget([
  2. 'name' => 'input-33',
  3. 'clientOptions' => [
  4. 'alias' => 'decimal',
  5. 'groupSeparator' => ',',
  6. 'autoGroup' => true
  7. ],
  8. ]);


Masked  URL input 

  1. echo MaskedInput::widget([
  2. 'name' => 'input-35',
  3. 'clientOptions' => [
  4. 'alias' => 'url',
  5. ],
  6. ]);


Masked  Email input 

  1. echo MaskedInput::widget([
  2. 'name' => 'input-36',
  3. 'clientOptions' => [
  4. 'alias' => 'email'
  5. ],
  6. ]);

Masked  Other Phone | US Phone input 


  1. echo MaskedInput::widget([
  2. 'name' => 'input-5',
  3. 'mask' => ['99-999-9999', '999-999-9999']
  4. ]);

More



Yii2 Bootstrap DatePicker Widget for Yii2

/yii2-date-picker-widget



Related Links

Yii Tabular Input



No comments:

Post a Comment