martin suchodol
martin suchodol

Reputation: 37

js format string to pretty url

since I'm not so proficient in js so I apologize in advance for a possibly unnecessary question, but as with js, rewrite plain text to pretty url.

So this: hello world

To this: hello-world

And then insert the rewritten form into the form input

<div class="form-group">
        <label for="title">Nadpis</label>
        <input type="text" name="title" id="title" class="form-control" value="{{ old('title') }}" required minlength="3" maxlength="80" onblur="this.form.url.value=this.value"/>
    </div>

    <div class="form-group">
        <label for="url">URL</label>
        <input type="text" name="url" id="url" class="form-control" value="{{ old('url') }}" required minlength="3" maxlength="80" readonly/>
    </div>

Upvotes: 2

Views: 1895

Answers (3)

mplungjan
mplungjan

Reputation: 177692

  • Add event listeners
  • Execute on load if needed
  • Remove inline event handlers
  • Perform a replace before URIEncoding

Change encodeURIComponent(this.value.replace(/ /g, "-")) to this.value.replace(/ /g, "-") if you just want the replace

Additionally we could use new URL() and URL SearchParams on the result if it is supposed to be an actual URL

//const re1 = /[\;\,\/\?\:\@\&\=\+\$\_\.!\~\*\'\(\)\#]/g
const re2 = /[^a-zA-Z0-9 ]/g

window.addEventListener("load", function() { // on page load
  const title = document.getElementById("title"); // store the field
  title.addEventListener("input", function() { // on any input 
    document.getElementById("url").value = encodeURIComponent(
      // this.value.replace(/ /g, "-").replace(re1,"") // left out the "-"
      this.value.replace(re2,"").replace(/ /g, "-") // using re 2
    ); // encode after replace
  });
  title.dispatchEvent(new Event('input')); // trigger the change
});
<div class="form-group">
  <label for="title">Nadpis</label>
  <input type="text" name="title" id="title" class="form-control" value="Hello world;,/?:@&=+$_.!~*'()#" required minlength="3" maxlength="80" />
</div>

<div class="form-group">
  <label for="url">URL</label>
  <input type="text" name="url" id="url" class="form-control" value="What is the old URL?" required minlength="3" maxlength="80" readonly/>
</div>

Upvotes: 1

barzin.A
barzin.A

Reputation: 1582

if i understand your problem, i hope this code helps you

function prettyUrl(value)
{
  return value.replace(/ /g, "-").replace(/@/g, "").replace(/\$/g, "").replace(/!/g, "").replace(/#/g, "").toLowerCase();
}
<form>
  <div class="form-group">
    <label for="title">Nadpis</label>
    <input type="text" name="title" id="title" class="form-control" required minlength="3" maxlength="80" onblur="this.form.url.value=prettyUrl(this.value)" />
  </div>

  <div class="form-group">
    <label for="url">URL</label>
    <input type="text" name="url" id="url" class="form-control" required minlength="3" maxlength="80" readonly/>
  </div>
</form>

Updated replace special character with Nothing and lowercase all letters

Upvotes: 1

Ali Kianoor
Ali Kianoor

Reputation: 1243

You can use this replace code to format your Pretty URL:

var prettyUrl = oldUrl.split(' ').join('-');

Upvotes: 0

Related Questions