0

I have created an alert box which will output the details from the form I am now wondering how I start the Java validation part so that if the form is filled in correctly an alert box with the form details will show however if it is wrong instruct the user to fill in the invalid boxes.

HTML

 <form id="foo" onsubmit="formAlert(); return false;" method="POST">
    <p><label for="first_name">First Name<label><input type="text" id="first_name" value="" /></p>
    <p><label for="last_name">Last Name<label><input type="text" id="last_name" value="" /></p>
     <p><label for="Phone_num">Phone Number<label><input type="number" id="phone_num" value="" /></p>


    <p><input type="submit" value="click me" onclick=""/></p>
</form>

Java Script

 function formAlert() {
alert_string = '';
var userName = document.getElementById('first_name').value;
var userLastName = document.getElementById('last_name').value;
 var phoneno =  document.getElementById('phone_num').value;
if(userName === "" || userName === null){
  alert("Please enter name");
}
if(userLastName === "" || userLastName === null){
  alert("Please enter lastname");
}

else if(phoneno === "/^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;" || phoneno === null){ alert("Please enter Phone number"); }

else{
 alert_string += userName + " ";
 alert_string += userLastName;
 alert_string += phoneno;
 alert(alert_string);

} }

Nic
  • 6,211
  • 10
  • 46
  • 69
  • Some punctuation of the question would make reading easier. There are [*many questions on form validation already*](http://stackoverflow.com/search?q=[javascript]+form+validation), what have you tried? – RobG Mar 17 '17 at 01:16
  • 1
    Java has nothing to do with JavaScript. – epascarello Mar 17 '17 at 01:16
  • So add if statements to check if they are valid or use html5 validation. – epascarello Mar 17 '17 at 01:17

2 Answers2

0

Here's a quick and simple solution. Add this to your javascript code. Hope it helps!

function validatePhone(inputtxt) {
 var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
 return phoneno.test(inputtxt)
 }

 function formAlert() {
  alert_string = '';
 var userName = document.getElementById('first_name').value;
 var userLastName = document.getElementById('last_name').value;
 var phoneno =  document.getElementById('phone_num').value;
 if(userName === "" || userName === null){
 alert("Please enter name");
 }
 if(userLastName === "" || userLastName === null){
 alert("Please enter lastname");
 }
else if(!validatePhone(phoneno)){ alert("Please enter 10 digits for phone  number"); }

else{
alert_string += userName + " ";
alert_string += userLastName + " ";
alert_string += phoneno;
alert(alert_string);
 } 
}

To check for email simple use required. Something like this:

<input type="email" name="email" required>

There are many ways to validate phone number. Here's a good example: Credit goes to: https://stackoverflow.com/a/18376246/4084160

Community
  • 1
  • 1
HenryDev
  • 4,685
  • 5
  • 27
  • 64
0

There is other way to do that. it is a good approach if you have many inputs

<html>

<style>
.required 
{
    border: 1px solid #F00;
    padding: 2px;
}
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="foo" onsubmit="formAlert(); return false;" method="POST">
        <p><label for="first_name">First Name<label><input                    type="text" id="first_name" value="" /></p>
        <p><label for="last_name">Last Name<label><input type="text"          id="last_name" value="" /></p>

        <p><input type="submit" value="click me" onclick=""/></p>
    </form>

    <div id="msgRequired" style="display: none;">
    <p>required Field!</p>
    </div>

<script>    

function formAlert() {
        alert_string = '';
        var cont = 0;
         $("#foo input").not(':hidden').each(function () {
        if ($(this).val() == "") {
            $(this).addClass("required");
            cont++;
        }
        else
        {
            $(this).removeClass("required");
        }
    });
        if(cont == 0)
        {

        alert_string = alert_string + document.getElementById('first_name').value;
        alert_string = alert_string + ' ';
        alert_string = alert_string + document.getElementById('last_name').value;

        alert(alert_string);
        $("#msgRequired").hide();
        }
        else
        {
        $("#msgRequired").show();
        }

       }
</script>   

</html>