You can use pattern for validation.

<input type="password" id="pass" name="pass" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters">

You can learn more here

However, if you ever wanted to use some javascript, then HTML5 does enable some validation, even pattern matching, but I'm pretty sure you'd need some javascript to have this working and also compare the two values with confirm password field as well.

You can try this

<input id="password" name="password" type="password" pattern="^\S{8,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Must have at least 8 characters' : ''); if(this.checkValidity()) form.password_two.pattern = this.value;" placeholder="Password" required>

<input id="password_two" name="password_two" type="password" pattern="^\S{8,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Please enter the same Password as above' : '');" placeholder="Verify Password" required>
Answer from Namwanza Ronald on Stack Overflow
🌐
W3Schools
w3schools.com › howto › howto_js_password_validation.asp
How To Create a Password Validation Form
Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete Turn off autocomplete Turn off spellcheck File Upload Button Empty Input Validation · Filter List Filt
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › input › password
<input type="password"> - HTML - MDN Web Docs
Many browsers now implement mechanisms to warn against insecure login forms. The value attribute contains a string whose value is the current contents of the text editing control being used to enter the password. If the user hasn't entered anything yet, this value is an empty string (""). If the required property is specified, then the password edit box must contain a value other than an empty string to be valid...
🌐
The Art of Web
the-art-of-web.com › javascript › validate-password
Password Validation using regular expressions and HTML5
We've also added a tricky little onchange handler to the first password field which updates the pattern required by the second password field - in effect forcing them to be identical: Here you can see a screen shot from Safari of the form being completed. The red/green markers have been implemented using CSS: In this example it should be clear to the user that the form can only be submitted once all three green ticks appear. In any case browsers such as Firefox and Opera will enforce the HTML5 validation rules and present messages as shown here:
🌐
Medium
medium.com › @sharathchandark › password-validation-check-in-html-css-javascript-show-hide-password-toggle-c57ebde8eb8e
Password Validation Check in HTML CSS & JavaScript | Show Hide Password Toggle | by sharathchandark | Medium
December 31, 2024 - In this article, we’ll walk you through a step-by-Step guide to building a fully functional password validation check from scratch using HTML, CSS and of course, JavaScript.
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-password-validation-form-with-css-and-javascript
How to create a password validation form with CSS and JavaScript?
A regular expression is set for the pattern to validate what user enters − ... The message that would be visible to the users while a password is set is placed using the title attribute − · title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" A check field is set that would be displayed if a user will type an incorrect form of password −
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Element › input › password
<input type="password"> - HTML: HyperText Markup Language
Many browsers now implement mechanisms to warn against insecure login forms. The value attribute contains a string whose value is the current contents of the text editing control being used to enter the password. If the user hasn't entered anything yet, this value is an empty string (""). If the required property is specified, then the password edit box must contain a value other than an empty string to be valid...
🌐
GeeksforGeeks
geeksforgeeks.org › html › validate-a-password-using-html-and-javascript
Validate a password using HTML and JavaScript - GeeksforGeeks
September 30, 2024 - Display Output: The result is displayed in a readonly input field with id="t2", showing whether the password is valid or not. Example: In this example we are following above-explained approach. ... <!DOCTYPE html> <html> <head> <title>validate password</title> <script type="text/javascript"> function test_str() { let res; let str = document.getElementById("t1").value; if (str.match(/[a-z]/g) && str.match( /[A-Z]/g) && str.match( /[0-9]/g) && str.match( /[^a-zA-Z\d]/g) && str.length >= 8) res = "TRUE"; else res = "FALSE"; document.getElementById("t2").value = res; } </script> </head> <body> <p> String: <input type="text" placeholder="abc" id="t1" /> <br /> <br /> <input type="button" value="Check" onclick="test_str()" /> <br /> <br /> Output: <input type="text" id="t2" readonly /> </p> </body> </html>
Find elsewhere
Top answer
1 of 4
9

Firstly - don't validate credentials like this - do it on the server side as anything client side is practically worthless for security.

Other than that...

  • Use css to change the presentation of elements rather than html elements like font.
  • javascript.validate(...) should be javascript:validate(...) or just validate(...).
  • Use <script type="text/javascript"> to start a (java)script element.
  • location href should be location.href
  • You need to wrap string values in quotes (i.e. workshop should be "workshop").
  • Change name="text1" to id="text1" and then you can get the value using document.getElementById("text1").value.

<form>
    <input type="text" placeholder="Username" id="text1" /><br />
    <input type="password" placeholder="Password" id="text2" /><br />
    <input type="button" value="Login"  onclick="javascript:validate()" />
</form>
<script type="text/javascript">
function validate()
{
    if(   document.getElementById("text1").value == "workshop"
       && document.getElementById("text2").value == "workshop" )
    {
        alert( "validation succeeded" );
        location.href="run.html";
    }
    else
    {
        alert( "validation failed" );
        location.href="fail.html";
    }
}
</script>

2 of 4
3

Since you are new on web development, i have decided to post this answer, that you would get useful advises. But first, I am curious if you are using a tutorial. If you are using a tutorial, please look for an another one since your HTML contains "ancient" tags.

This post contains of three parts: HTML, CSS, then JavaScript.

About HTML

Let's start with the HTML first. Here is an example of what you have written:

<font size=5>
    <font color="black">
        <input type="text" placeholder="Username" name="text1">
        <br /><br />
    </font>

When you want to use a tag, you can open it by <tagname>. But some tags require that you have to close it. You can close a tag with </tagname>

This example is a bad example

<div>
    // other inner HTML tags

This is a good example:

<div>
    // other inner HTML tags.
</div>

You can see that i have added </div> to close the div-element. The modern browsers is able to "solve" it for you, when parsing the HTML through the DOM, but it is best to do it yourself.

However, there are tags which contains multiple attributes. Like the input element.

<input type="text" placeholder="Username" name="text1" />

Please notice the added / at the end of the tag. I also recommend to use convenient name for the input field. If you expect it to be used for user name, please call it by "username". Here is an improved example:

<input type="text" placeholder="Username" name="username" id="username" />

If you want to read more about HTML, please read this documentation about HTML. Try to avoid old (not used anymore) tags like <font> or even <! javascript>. Where did you have dug this one?

About CSS

You have used <font> in your HTML. As said in the previous section, it is not used anymore. To style the HTML, we are using CSS now. That stands for "Cascading Style Sheets" which allows you to style a plain HTML document in a nice looking webpage. If you want to read more about css, you can use this documentation.

In css, if you want to style the font, you can use for example

input[type="text"] {
    color: red;
}

This CSS part says "search for input[type="text"] elements". Then color the text-color as red. This is a simple example. By using CSS, you can manipulate the look of your webpage. If you want to learn more about CSS, please use the given documentation, or use this tutorial.

About JavaScript

What you have entered is good, but you still need to check for the basics. Let's look to your first function: validate.

function validate(text1,text2)
{
    if(text1==workshop && text2==workshop)
        load("run.html");
    else
    {
        load("fail.html");
    }
}

Your condition part is text1==workshop && text2==workshop. The variables text1 and text2 is known (passed by the function as argument). But what about workshop ? Did you have defined it somewhere ? If not, then this code would fail, because the script will look for a variable workshop that doesn't exist.

I suppose that you want to search by string name:

text1=="workshop"

Which is a valid condition check. The script will see if text1 matches the given string.

Now, about coding style. You are firstly not using { and } at the first if, but you are using that at your else. Try to handle one coding style. Either you are using { and } at both or not even one.

For example, with { and }:

if(text1=="workshop" && text2=="workshop") {
    load("run.html");
}
else {
    load("fail.html");
}

Or without

if(text1==workshop && text2==workshop)
    load("run.html");
else
    load("fail.html");

Both are better readable then what you have entered. If your condition block is multi-line, you have to use { and } of course.

if( condition ) {
    // line 1
    // line 2
}

This means that line1 and line2 will be executed if the condition is true.

Now, the function usage. You have used

function load(url)
{
    location href=url;
}

This function doesn't work. It will give you a runtime error. location.href is what you need. If you want to use native JavaScript functions, ensure that you have named it correctly.

Back to your question. You want to check the entered data when you have clicked the button. This is possible by using "events". You have used onclick in the button tag

<input type="submit" value="Login" onclick="validate()" />

Don't worry, this will work. (i'm only not a fan of this style). If you click on the button, it will run the "validate()" function. There is no need to add "javascript" before it. The DOM can handle it. The function name and its argument must be same as the entered string in the onclick attribute. If you don't do this, the script will search for a non existing function, thus leading to an error. Since we are using validate without arguments, we have to redefine the function as below.

function validate()
{
    if(text1=="workshop" && text2=="workshop")
        // code omitted
}

Please recall that text1 is a variable and has to be defined. Since these aren't passed by methods argument, you have to select the input tags, to ensure that you can obtain the data required. This can be done by document.getElementById() function, which selects an element by its id.

var text1 = document.getElementById("username");

This says "get the element by its id: username". Please check the HTML section of the example. You can find out that the input element has an id="username" attribute.

But we are not there yet ! The function document.getElementById gives you a node of the element with the given ID. Since the element is an input field, you can use the .value property to obtain its current value. Do the same for text2 (the password field, then you have a working function !

function validate()
{
    var text1 = document.getElementById("username");
    var text2 = document.getElementById("password");
    if(text1.value=="workshop" && text2.value=="workshop") {
        load("run.html");
    }
    else {
        load("fail.html");
    }
}

There are other ways to select your tags in the document: getElementByClassName, querySelector, querySelectorAll and more. Please check for its documentation of how it works.

That's all what you need to ensure that your code works. If you want to know more about javascript, please read this documentation. Or start learning the basics by using this tutorial.

Please don't copy paste my answer directly. Try to learn more about web developing. It's an amazing world and it can help yourself more.

But, still a side note: you are validating data on client side. I would not recommend that because it is not security safe. Usually the form should be submitted to a server, which then validates the form. If the entered data is valid, the server will redirect the user to the right page. This can be done with PHP, ASP.NET, ...

full example

    function validate() {
      var text1 = document.getElementById("username");
      var text2 = document.getElementById("password");
      if (text1.value == "workshop" && text2.value == "workshop") {
        load("run.html");
      } else {
        load("fail.html");
      }
    }

    function load(url) {
      location.href = url;
    }
input[type="text"] {
  color: red;
}
<form>
  <input type="text" placeholder="Username" name="username" id="username" />
  <br />
  <br />
  <input type="password" placeholder="Password" name="password" id="password" />
  <br />
  <br />
  <input type="submit" value="Login" onclick="validate()" />
</form>

🌐
W3Resource
w3resource.com › javascript › form › password-validation.php
JavaScript : Password validation - w3resource
August 19, 2022 - To validate the said format we use the regular expression ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$. Next the match() method of string object is used to match the said regular expression against the input value.
🌐
CodePen
codepen.io › diegoleme › pen › qBpyvr
Confirm password with HTML5
<form class="pure-form"> <fieldset> <legend>Confirm password with HTML5</legend> <input type="password" placeholder="Password" id="password" required> <input type="password" placeholder="Confirm Password" id="confirm_password" required> <button type="submit" class="pure-button pure-button-primary">Confirm</button> </fieldset> </form> ... var password = document.getElementById("password") , confirm_password = document.getElementById("confirm_password"); function validatePassword(){ if(password.value != confirm_password.value) { confirm_password.setCustomValidity("Passwords Don't Match"); } else { confirm_password.setCustomValidity(''); } } password.onchange = validatePassword; confirm_password.onkeyup = validatePassword;
🌐
CodingNepal
codingnepalweb.com › home › javascript › password validation check in html css & javascript
Password Validation Check in HTML CSS & JavaScript
May 4, 2023 - This includes creating a password checklist, adding the necessary HTML elements to the form, styling the elements using CSS, and adding JavaScript code to validate the password and toggle password visibility.
🌐
Upgrad
upgrad.com › home › blog › software development › everything you need for effective password validation in javascript!
Enhance Security with Password Validation in JavaScript Now!
July 9, 2025 - Special Characters: Require symbols such as !@#$%^ for randomness. Validate using JavaScript RegEx like /[!@#$%^&*]/ and style input using ... No Spaces Allowed: Reject passwords with spaces to prevent formatting issues. Use .includes(" ") check in JavaScript, with HTML input restrictions.
🌐
YouTube
youtube.com › watch
Password Validation Check in HTML CSS & JavaScript | Password Strength Check in JavaScript - YouTube
In this video, you will learn how to check password validation in HTML, CSS, and JavaScript. By using this password validation checker, you can easily enter ...
Published   June 19, 2023
🌐
Envato Tuts+
webdesign.tutsplus.com › home › web design › html/css › javascript for designers
Write Your Own Password Validation with JavaScript | Envato Tuts+
September 8, 2023 - HTML/CSS JavaScript for Designers · Let’s write our very own password validation. It will include features such as toggling password display, matching a confirm password field, and disabling form submission until the password is valid. Client-side validation is one of the most important features that can be done with JavaScript.
🌐
YouTube
youtube.com › watch
How to make Login form with password validation using HTML CSS & JS | Step-By-Step Tutorial | PraRoz - YouTube
#html #css #LoginformvalidationIn this video you will learn to make a LOGIN FORM with Password validation i.e at least 8 character needed to LOGIN . and afte...
Published   August 25, 2021
🌐
Coding Jasim
codingjasimweb.com › home › blog › validation of password in html css and javascript
Validation of Password in HTML CSS and JavaScript
December 18, 2025 - In this blog, I showed you how to set up a password validation using HTML, CSS, and JavaScript.
🌐
DEV Community
dev.to › codehuntersharath › password-validation-check-in-html-css-javascript-show-hide-password-toggle-4d7c
Password Validation Check in HTML CSS & JavaScript | Show Hide Password Toggle - DEV Community
December 31, 2024 - In this article, we'll walk you through a step-by-Step guide to building a fully functional password validation check from scratch using HTML, CSS and of course JavaScript.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › password-validation-form-using-javascript
Password Validation Form Using JavaScript - GeeksforGeeks
April 18, 2025 - <!-- index.html file --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Password Validation Form using javaScript</title> </head> <body> <div class="container"> <h3>Password Validation Form using JavaScript</h3> <form action="#"> <label for="username">Username</label> <input type="text" id="username" name="username" required> <label for="password">Password</label> <input type="password" id="password" name="p