0

I want to restrict the use of comma in textbox. Initially the textbox is disabled and only when the user clicks on a related checkbox, the textbox is enabled.

Here is the code I have tried:

function checkForComma(keyCode) {
    if (event.keyCode == 188) {
        alert('Not allowed');
    }
}

And the EditorTemplate:

@Html.TextBoxFor(m => m.AddressLine1, @Model.isUsed ? 
    (object)new { @class = "form-control" } : 
    new { @class = "form-control", @disabled = "disabled", @onkeypress = "checkForComma(this);" })

@Html.ValidationMessageFor(m => m.AddressLine1)

1 Answer 1

2

you forgot to place return false in your function. modify you function following

function checkForComma(event) {
    if (event.charCode == 44) {
        alert('Not allowed');
        return false;
    }else{
        return true;
    }
}

Also modify your html code as following

@Html.TextBoxFor(m => m.AddressLine1, @Model.isUsed ? 
    (object)new { @class = "form-control" } : 
    new { @class = "form-control", @disabled = "disabled", @onkeypress = "return checkForComma(event);" })
Sign up to request clarification or add additional context in comments.

6 Comments

Does your alert message display?
No nothing is displayed
how can I allow comma in the textbox and restrict it at the end of text entered
you need to check that on any button click or on blur of text box.
can you give an example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.