Jump to content

Form validation using JavaScript

ROck_

I have an assignment to validate the form using JavaScript,Please help me out here,why is it not working?

I am still trying to figure this out so its not complete yet.

 

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
    .container {
        width: 500px;
        clear: both;
    }
    .container input {
        width: 100%;
        clear: both;
    }
	body{
	background-image:url('268641.jpg');}

    </style>
<script type="text/javascript">
function c()
{

var mytext=document.getElementById('textarea').value;
	if(document.getElementById('name').value==0)
	{
	alert('Please Enter Your Name');
	}
var contact=document.getElementById("contact").value;
var contactnum=/^\d{10}$/;
	if((contact.value.match(contactnum))  
        {  
      return true;  
        }  
      else  
        {  
        alert("Invalid Contact Number");  
        return false;  
        }  
}
</script>
</head>
<body>
<div class="container">
<form onsubmit="c()">
<label>Name:</label><input type="text" id="name" maxlength="50"><br/><br/>
<label>Contact:</label><input type="text" id="contact" maxlength="10"><br/><br/>
<label>E-mail:</label><input type="text" id="email"><br/><br/>
Gender <br/>
<input style="width: 22px;" type="radio" name="gender" value="male">Male<br/>
<input style="width: 22px;" type="radio" name="gender" value="female">Female<br/>
<input style="width: 22px;" type="radio" name="gender" value="other">Other<br/><br/>
<br/>
<br/>
<label>Comment:</label><textarea id="textarea" rows="4" cols="50">

</textarea><br/>
<input type="submit" value="Submit" >

</form>
</div>
</body>
</html>

 

Link to comment
Share on other sites

Link to post
Share on other sites

You can check if a string is empty by doing if(!someString) as empty strings evaluate as false.

 

EDIT: Aha, that's probably not the problem, but I did run your JS through a linter

 

Quote
5 Use '===' to compare with '0'.
6 Use '===' to compare with '0'.
13 Expected ')' to match '(' from line 12 and instead saw '{'.
16 Expected an identifier and instead saw 'else'.
16 Expected an assignment or function call and instead saw an expression.
16 Missing semicolon.

So you're missing a ) at the end of this statement

 if((contact.value.match(contactnum))

 

Link to comment
Share on other sites

Link to post
Share on other sites

Also onsubmit="return c()"

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

yea I corrected that,the name validation works,contact one never runs.

function c()
{

var mytext = document.getElementById("textarea").value;
var myname = document.getElementById("name").value;
	if(myname.length==0)
	{
	alert("Please Enter Your Name");
	}
var contact=document.getElementById("contact").value;
var contactnum=/^\d{10}$/;
	if(contact.value.match(contactnum))
        {  
      return true;  
        }  
      else  
        {  
        alert("Invalid Contact Number");  
        return false;  
        }  
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×