[RESOLVED]Masking for Textboxfor Helper in MVC 4.

Hello All,

I am trying to mask a textboxfor helper of mvc 4 razor syntax using javascript.But it isnt working.Can anyone please guide me ? I want to use onkeypress kind of event so that the user can understand while typing.

@Html.TextBoxFor(model => model.AuxiliaryProgramMappingDTO[i].BreakDuration, new { id = "BreakDuration" + Model.AuxiliaryProgramMappingDTO[i].BreakTypeid.ToString(),  @placeholder = Model.AuxiliaryProgramMappingDTO[i].BreakDuration, @class = "input-medium" })

After defining the textbox in Razor, you need to invoke the javascript code. Assuming that you are using mask javascript and already include the script file in the code

$('input.input-medium').setMask({ mask: '999-999-9999' });

For more information please check this

Masking in MVC 4 using Data Annotation


$('input.input-medium').setMask({ mask: '999-999-9999' });

Not working.No change.

Ashim Chatterjee

Not working.No change.

Please elaborate what have you done.

You need to do the following to show the masked textbox

1. In the view use the TextBoxFor helper (which you have already done)

2. Include the javascript plugin which does the masking

3. On the document load, call the mask (similar to the javascript code I provided earlier)

Also please check the link I posted earlier.

You can try using the Jquery Plugins to mask your textbox  digitalBush Masking Plugin.

Sample Code:

 <script type="text/javascript">
             jQuery(function ($) {


Leave a Reply