mandatory use of a word in an html form

Asked

Viewed 44 times

-3

I have an input where the user writes a title, I need this title to have added (blog) to its end.

example: my title (blog) or your blog title

if it does not, I need to add this automatically or prevent sending the form with javascript or other language.

  • the software uses php, is a Wep app, the title will be saved in database.
  • 1

    If you consider adding this automatically, you can relieve the user of this obligation and add it in PHP itself before recording it in the database. Something like: $titulo .= ' blog';

  • How would you do on the Laravel? i was considering using an html form, in which case I would have to take the exact value of the field in the validation to then add the tag (blog).

  • 1

    I don’t know anything about Laravel :/

  • tranquil, php works too, I do a way here.

1 answer

1


So we can make things easier by using javascript, for example, you can check if the value exists, if it exists, let it pass, otherwise, add, is a good one? To check using javascript we can use the string.indexOf("blog") != -1 if it does not contain "blog" in the string, it returns -1, if it does, it returns the position of the string.

If you are using an input, we can take the value and check each typed letter, we can trigger a function to each typed letter by adding the tag onkeyup for example, where we would check letter to letter.

Example for checking, remembering that use jquery to take the amount:

<input type="text" id="texto" onkeyup="checar()">

<script type="text/javascript">
  function checar(){
    if($("#texto").val().indexOf("blog") != -1){return true;}
  }
</script>

We can check at the time of sending the form, and thus add in the string.

  • 1

    good, it has problem be with jquery no, I am using bootstrap in form.

  • 1

    Okay, it’ll be easy to do the check so you can even do this substitution on this function itself

  • 2

    You don’t need else, because if you enter the if won’t even get into the return false.

  • 1

    True friend, but the idea is he edit the function and adapt his reality. I will edit, in any case

Browser other questions tagged

You are not signed in. Login or sign up in order to post.