Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

How to validate a form in JavaScript

3 Answers

0 votes
<html>
<head>
<title>Form Validation With JavaScript</title>

<script>
function validateForm() 
{
    var nm = document.forms["formname"]["firstname"].value;
    
    if (nm == "" || nm == null) 
    {
        alert("Please write a name");
        return false;
    }
}
</script>

</head>
<body>
  <form name="formname" action="add-user.php" onsubmit="return validateForm();" method="post">
    Name: <input type="text" name="firstname">
    <input type="submit" value="Submit">
</form>
</body>
</html>

 



answered Jun 28, 2015 by avibootz
0 votes
<html>
<head>
<title>Form Validation With JavaScript</title>

<script>
function validateForm() 
{
    var nm = document.getElementById("firstname-id").value;
    
    if (nm == "" || nm == null) 
    {
        alert("Please write a name");
        return false;
    }
}
</script>

</head>
<body>
  <form name="formname" action="add-user.php" onsubmit="return validateForm()" method="post">
    Name: <input id="firstname-id" type="text" name="firstname">
    <input type="submit" value="Submit">
</form>
</body>
</html>

 



answered Jun 28, 2015 by avibootz
0 votes
<!-- test.html -->

<html>
<head>
<title>Form Validation With JavaScript</title>
<script type="text/javascript" src="test.js"></script> 
</head>

<body>
  <form name="formname" action="add-user.php" onsubmit="return validateForm()" method="post">
    Name: <input id="firstname-id" type="text" name="firstname">
    <input type="submit" value="Submit">
</form>
</body>
</html>
// test.js

function validateForm() 
{
    var nm = document.getElementById("firstname-id").value;
    
    if (nm == null || nm == "") 
    {
        alert("Please write a name");
        
        return false;
    }
}

 



answered Jun 28, 2015 by avibootz

Related questions

2 answers 272 views
272 views asked Nov 19, 2018 by avibootz
1 answer 241 views
1 answer 196 views
1 answer 245 views
245 views asked Jun 28, 2015 by avibootz
...