Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to do javascript form validation

Featured Replies

Ok so for people who dont wanna really use php for validation which might be considered alittle bit harder imma show u an easy way to do it in javascript below we will use 3 fields name,email,password

 

<html>

<head>
 <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
 <title>validation</title>

</head>
<body>
<script language="JavaScript" type="text/javascript">
function validate(){
if(document.form.name.value.length < 1){
alert("Please enter your name");
return false;

}
 if(document.form.email.value.length < 1){
alert("Please enter your email");
return false;

}
 if(document.form.password.value.length < 1){
alert("Please enter your a password");
return false;

}
}
</script>
<form name="form" method="POST" action="test.html" onSubmit="return validate();">
<p><b>Name</b><br /><input type="text" name="name" size="30"></p>
<p><b>Email</b><br /><input type="text" name="email" size="30"></p>
<p><b>Password</b><br /><input type="password" name="password" size="30"></p> 
<input type="submit" name="submit" value="Submit"> 
</form>
</body>
</html>

You may also do this as messages instead of pop ups by changing the JavaScript to

 

 


<script language="JavaScript" type="text/javascript">
function validate(){
if(document.form.name.value.length < 1){
var message ="I am sorry you must fill in your name";
document.write(message);
return false;

}
 if(document.form.email.value.length < 1){
var message ="I am sorry you must fill in your email";
document.write(message);
return false;

}
 if(document.form.password.value.length < 1){
 var message ="I am sorry you must fill in your password";
document.write(message);
return false;

}
}
</script>

But anyways i do hope this is useful to some people if you don't wanna do it in php but um incase you do i my as well show you that also lol heres the php code to validate using php.

 

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$errors = array();
if(isset($_POST['submit'])){
if(!$name){
$errors[] = "I am sorry you must fill in your name";

}
if(!$email){
$errors[] = "I am sorry you must fill in your email";

}
if(!$name){
$errors[] = "I am sorry you must fill in your password";

}

if(count($errors) > 0){

foreach($errors AS $error){
echo "$error <br />";

}
}else{
//If no errors do this


//If no errors do this
}
}

?>

Ok well as you can see javascript is a little bit easier to do validation well i hope you enjoyed this and find it useful :clapping:

Great post, I'm sure people will find this very useful :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.