0

This is My Form Input where I am using.

                     <div class="row">
                        <div class="col-12">
                          <div class="form-group" id="title_of_class_form">
                            <label class="control-label">Title of Class: </label>
                            <div class="col-md-12">
                              <input type="text" name="title_of_class" id="title_of_class" 
class="form-control"
                                autocomplete="off" />
                            </div>
                            <span style="color:red;font-size:12px;" id="errNm1"></span>
                          </div>
                        </div>
                      </div>
                      <div class="row">
                        <div class="col-12">
                          <div class="form-group" id="classes_link">
                            <label class="control-label">Slug (Permalink): </label>
                            <div class="col-md-12" id="classes_link">
                              <input type="text" name="classes_link" placeholder="" val=""  
class="form-control" />
                            </div>
                            <span style="color:red;font-size:12px;" id="errNm1"></span>
                          </div>
                        </div>
                      </div>

This is My Script Tag. In this code When I am replacing one String then my code is working but when I am using multiple Special characters the not working.

<script>
$('#title_of_class').keyup(function () {
 var title = $(this).val();
 title_link = title.replaceAll([' '], '-');
  title_link_data = title_link.replaceAll(['!','@','|','1','2','3','4','5','6','7','8','9','0','/','[',']','{','}',')','(','#','$','%','^','&','*','+','=',' '], '-');
  $('#classes_link input').val(title_link_data);
  console.log('#classes_link'+ title_link_data);
  });
 </script>

2 Answers 2

1

You can use regex to match only alphabets -

Regex - /[^a-zA-Z]/g

$('#title_of_class').keyup(function() {
  var title = $(this).val();
  title = title.replaceAll(/[^a-zA-Z]/g, '-');
  $('#classes_link input').val(title);
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<div class="row">
  <div class="col-12">
    <div class="form-group" id="title_of_class_form">
      <label class="control-label">Title of Class: </label>
      <div class="col-md-12">
        <input type="text" name="title_of_class" id="title_of_class" class="form-control" autocomplete="off" />
      </div>
      <span style="color:red;font-size:12px;" id="errNm1"></span>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-12">
    <div class="form-group" id="classes_link">
      <label class="control-label">Slug (Permalink): </label>
      <div class="col-md-12" id="classes_link">
        <input type="text" name="classes_link" placeholder="" val="" class="form-control" />
      </div>
      <span style="color:red;font-size:12px;" id="errNm1"></span>
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

You can't use array with replaceAll

Syntax

replaceAll(regexp, newSubstr)
replaceAll(regexp, replacerFunction)
replaceAll(substr, newSubstr)
replaceAll(substr, replacerFunction)

Use regex instead

a = '!"#$%&()0=~|^'
b = a.replaceAll(/[!,@,|,1,2,3,4,5,6,7,8,9,0,/,\[,\],{,},),(,#,$,%,^,&,*,+,=,| ]/g,'_')
console.log(b)
_"________~__

Comments

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.