Strip html tags before callback

2019-08-23 22:22发布

问题:

I use AjaxHelper.BeginForm to create ajax form in my view. The view has a textarea to provide content. How can I remove html tags from this textarea before form is submited. What I mean:

  • user clicks 'button'
  • tags are stripped
  • callback is made.

回答1:

You can use a regular expression to remove HTML tags from a string, but note that it is not 100% reliable.

$("button").click(function() {
    var text = $("#someElement").val().replace(/(<([^>]+)>)/ig, "");

    // make callback with above variable...
});