Disregard words in input

Asked

Viewed 119 times

2

I would like to know how to override/disregard certain floods placed by users in the input.

I have an input where the person should send me the URL of a site, in case they should send the url as a site.com/image because I have a script that takes the rest of the information.

But it turns out that people input things like site.com/image1.jpg, site.com/image2.jpg site.com/image3.jpg and so the error.

My question is how do I UNDO CERTAIN WORDS IN INPUT like: imagem1.jpg, imagem2.jpg. So the person can send the wrong link but the script would disregard these series of wrong words. Or the scribe could replace and subistituite certain words by a blank space (tmb would resolve)

I tried this script : http://jsfiddle.net/6hxspv14/2/ But in practice it doesn’t work when I click the Submit button excuse the difficulty of expressing myself I’m inciando in programming .

my input code:

<form action="thumbs.php" method="post">
URL da thumb: <input type="text" class="restrict"  name="url"><br>
<input  type="submit">
</form>
  • But this script doesn’t have Submit...

  • Yes, I say I tested this script in my original code but it didn’t work

  • you want to undo certain words or a word pattern ?

  • It’s actually a pattern. 1.jpg 2.jpg 3.jjgp ... up to 30.jpg because users are inserting site/img/1.jpg so the result is site/img/1.jpg.1.jpg because I already got this parameter in php. so I need to override these in input

  • Can you input the input code ? In your question you have a [edit] link edit and put the relevant code, in the control bar you have the {keys}, select all your code and click the keys...

  • <form action="Thumbs.php" method="post"> Thumb’s URL: <input type="text" class="restrict" name="url"><br> <input type="Submit"> </form>

  • You can put in your question by editing ?

  • ready, put on ;)

Show 3 more comments

1 answer

1


The form that came to mind is using ER(expressão regular) and the method replace().

<form action="" method="post">
URL da thumb: <input type="text" class="restrict"  name="url" value=""/><br/>
<input  type="submit" onclick="replaceURL()" value="Enviar"/>
</form>
<script>
function replaceURL()
{	var inputURL = document.getElementsByClassName("restrict")[0].value;//captura o valor do input
	var replaceurl = inputURL.replace(/\.[\d]{1,2}\.jpg/, " ");//substitui o valor capturado pela "ER" por " "
	/* "ER" explicação
	 * "\." encontra o "." literalmente uma vez que ele é um operador é necessária a "\" para escapar e torná-lo literal
	 * "[\d]{1,2}" encontra o número em uma ou duas ocorrências dentro da lista [\d] que por sua vez é uma abreviação para os dígitos[0-9]
	 * "\.jpg" encontra a extensão(repara na "\" novamente para escapar o ponto)
	 *  */
	var inputFianl = document.getElementsByClassName("restrict")[0].value = replaceurl;//atribui o resultado da substituição ao valor do input
	alert("O valor atual do input é : "+inputFianl);//alerta o resultado final, só para você ver pois após o submit a pagina é recarregada pois o action=""
}	 
</script>

  • Thank you very much friend, it worked right. I thought I would have to use Jquery but its solution was extremely effective. It helped a lot!!

  • Consider validating one of the answers by clicking on the green incon below the rating arrows.. Good Luck...

Browser other questions tagged

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