I have to get a Message from the user which should not include any special characters.I assigned an id to this.
@Html.TextAreaFor(model => model.Message, new { id= "msg", htmlAttributes = new { @class = "form-control" } })
On clicking the Submit button, the message is sent. I use the previously mentioned id "msg" in the function of onclick event, as shown.
<input type="submit" value="Contact Us" class="btn btn-default" id="submit" onclick="return removeSpecialChar(msg)"/>
At the beginning of the same cshtml file, I also mentioned the function removeSpecialChar()
:
<SCRIPT type=text/javascript>
function removeSpecialChar(msg) {
msg = msg.replace(/[^\w\s]/gi, '');
return msg;
}
</SCRIPT>
However, the special characters are not being replaced in the message. Please help me in understanding the issue with this approach and how to resolve this? Also, suggest if it is necessary to use the namespace for regex?