Using JAVASCRIPT make this file use the following input validation The input val
ID: 3703813 • Letter: U
Question
Using JAVASCRIPT make this file use the following input validation
The input validation is in JAVASCRIPT
Form Validation:
In this assignment there will be 5 things to validate:
1- Validate that the names are filled in, first and last name
2- Validate that they are letters, upper case and lower case. (You can use /^[a-zA-Z]+$/ as the regular expression)
3- Check to make sure the phone number is all numbers. (You can use /^[0-9]+$/ as the regular expression)
4- If they are updating the form make sure the form is filled out completely.
5- If they are creating a new record make sure they filled the form out completely.
If any of these fields are not met then the form will produce an alert and let them know what they are missing.
add.php
<html>
<head>
<title>Add Data</title>
</head>
<body>
<?php
include_once("config.php");
if(isset($_POST['Submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$email = $_POST['email'];
$address = $_POST['address'];
$phonenumber = $_POST['phonenumber'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$username = $_POST['username'];
$password = $_POST['password'];
$birthdate = $_POST['birthdate'];
$relationship = $_POST['relationship'];
// checking empty fields
if(empty($name) || empty($lastname) || empty($age) || empty($email)|| empty($address)|| empty($phonenumber)|| empty($city)|| empty($state)|| empty($zip)|| empty($username)|| empty($password)|| empty($birthdate)|| empty($relationship)) {
if(empty($name)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
if(empty($lastname)) {
echo "<font color='red'>Last Name field is empty.</font><br/>";
}
if(empty($age)) {
echo "<font color='red'>Age field is empty.</font><br/>";
}
if(empty($email)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
if(empty($address)) {
echo "<font color='red'>Address field is empty.</font><br/>";
}
if(empty($phonenumber)) {
echo "<font color='red'>Phone Number field is empty.</font><br/>";
}
if(empty($city)) {
echo "<font color='red'>City field is empty.</font><br/>";
}
if(empty($state)) {
echo "<font color='red'>State field is empty.</font><br/>";
}
if(empty($zip)) {
echo "<font color='red'>Zip Code field is empty.</font><br/>";
}
if(empty($username)) {
echo "<font color='red'>Username field is empty.</font><br/>";
}
if(empty($password)) {
echo "<font color='red'>Password field is empty.</font><br/>";
}
if(empty($birhdate)) {
echo "<font color='red'>Birhdate field is empty.</font><br/>";
}
if(empty($relationship)) {
echo "<font color='red'>Relationship field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href="#">Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$result = mysqli_query($mysqli, "INSERT INTO users(name,lastname,age,email,address,phonenumber,city,state,zip,username,password,birthdate,relationship) VALUES('$name','$lastname','$age','$email','$address','$phonenumber','$city','$state','$zip','$username','$password','$birthdate','$relationship')");
//display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href="index.php">View Result</a>";
}
}
?>
</body>
</html>
------------------------------------------------------------------------------------------------------ index.php
<?php
include_once("config.php");
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<a href="add.html">Add New Data</a><br/><br/>
<a href="search.php">Search</a><br/><br/>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>First Name</td>
<td>Last Name</td>
<td>Age</td>
<td>Email</td>
<td>Address</td>
<td>Phone Number</td>
<td>City</td>
<td>State</td>
<td>Zip Code</td>
<td>Username</td>
<td>Password</td>
<td>Birth Date</td>
<td>Relationship</td>
<td>Update</td>
</tr>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['lastname']."</td>";
echo "<td>".$res['age']."</td>";
echo "<td>".$res['email']."</td>";
echo "<td>".$res['address']."</td>";
echo "<td>".$res['phonenumber']."</td>";
echo "<td>".$res['city']."</td>";
echo "<td>".$res['state']."</td>";
echo "<td>".$res['zip']."</td>";
echo "<td>".$res['username']."</td>";
echo "<td>".$res['password']."</td>";
echo "<td>".$res['birthdate']."</td>";
echo "<td>".$res['relationship']."</td>";
echo "<td><a href="edit.php?id=$res[id]">Edit</a> | <a href="delete.php?id=$res[id]">Delete</a></td>";
}
?>
</table>
</body>
</html>
Explanation / Answer
You can edit this logic as per your need
var checkAllfields ;
if ((Name == null || Name =='') || (firstName==null || firstName =='') || (lastName==null || lastName==''))
{
checkAllfields = "true";
alert (' Please checkif the names are not null!!')
}
else if(!(/^[a-zA-Z]+$/.test(Name)) || !(/^[a-zA-Z]+$/.test(firstName)) || !(/^[a-zA-Z]+$/.test(lastName)))
{
checkAllfields = "true";
alert (' Please input valid first and last name!!')
}
else if(checknum(phonenumber) == false)
{
checkAllfields = "true";
alert (' Please input valid mobile number!!')
}
function checknum(phonenumber)
{
var numbers = /^[0-9]+$/;
if(phonenumber.value.match(numbers))
{
return true;
}
else
{
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.