-2
<b>Gebruikersnaam:</b><br>
<input id="textbox" type="text" value="" size="25" required>
<a id="googleLink" href="##klik nu om member te kopen##" 
 onclick="this.href='https://websitelink.com?naam=' + encodeURIComponent(document.getElementById('textbox').value);">
  <button>Koop: MEMBER</button>
</a>

I want: <input id="textbox" type="text" value="" size="25" required>

to be required when i click: <button>Koop: MEMBER</button>

like: if <input id="textbox" type="text" value="" size="25" required> value=""

its shows an alert like: "No username entered"

Fixed, Thx Lepanto

Kylian105
  • 7
  • 1
  • 4

3 Answers3

1

You need to cleanup your code first. not sure why you're using <a>. Anyway It would be pretty good to validate using onblur()

HTML code:

<input id="textbox" type="text" value="" size="25" required onblur="validateTextBox()">

JavaScript:

function validateTextBox() {
    if (document.getElementById("textbox").value != "") {} else {
        alert("Please enter a value");
    }
}

Check this out in JSFiddle.

Praveen
  • 55,303
  • 33
  • 133
  • 164
0

Of course, there is no such thing as required attribute in HTML tag textarea.

You have to create your own validation methods using Javascript. One of them is Validation plugin (which requires jQuery).

Jaime Gris
  • 1,136
  • 6
  • 11
0

You have to write a JavaScript function to validation the textbox. Something like below

<script>
function doclick(){
    if(document.getElementById('textbox').value != ''){
        this.href='https://websitelink.com?naam=' + encodeURIComponent(document.getElementById('textbox').value);
    }
    else{
        alert('No username entered');
    }
}
</script>

And call this function in your HTML tag onclick

<b>Gebruikersnaam:</b><br>
<input id="textbox" type="text" value="" size="25" required>
<a id="googleLink" href="##klik nu om member te kopen##" onclick="doclick()">
<button>Koop: MEMBER</button>
</a>
Lepanto
  • 1,413
  • 1
  • 8
  • 15