Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:00:56+00:00 2026-05-26T23:00:56+00:00

I have a form with 4 fields First Name (required) Last Name (required) Email

  • 0

I have a form with 4 fields
First Name (required)
Last Name (required)
Email (required)
phone (optional) (if user enter any it should validate to a number or not) below is the form i have

<form name="myForm" method="post" onsubmit="return validate();">
First Name : <input type="text" name="fname" id="id_fname"> <br>
Last Name : <input type="text" name="lname" id="id_lname"> <br>
Email : <input type="text" name="email" id="id_email"> <br>
Phone : <input type="text" name="phone" id="id_phone"> <br>
<input type="submit" value="send">
</form>

And below is the javascript code

<script type="text/javascript">
function validate()
{
 if (document.myForm.id_fname.value == '') {
alert("Enter First Name"); 
return false;}

else if(document.myForm.id_lname.value == '') {
alert("Enter Last Name");
return false; }

// now here email validation is not working

else if(document.myForm.id_email.value == '' || document.myForm.id_email.value != ''){
var x = document.myForm.id_email.value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
    alert("Not a valid e-mail address");
    return false;
    else
    return true;
    } 

// this is for phone number, if this field is empty no validation, if this field not empty it should validate to contain only numbers

else if(document.myForm.id_phone != '') {
var ValidChars = "0123456789";
var IsNumber=true;
var Char;
sText = document.form1.testfield2_phone.value ;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}

if(IsNumber == false)
alert("Enter valid Number");
return false;
else
return true;
}


else
return true;
document.myForm.submit();
}
</script>

Email validation and phone validation are not working when i comment these email and phone i get validation good for first name and last name… is there any error in code for validation.

And when a form is submitted, when we refresh then details are resubmitted again, how to avoid this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:00:56+00:00Added an answer on May 26, 2026 at 11:00 pm

    Why do you do it so complicated???

    if (document.myForm.id_fname.value.length < 3) {
        alert("Enter First Name");
        return false;
    } else if (document.myForm.id_lname.value.length < 3) {
        alert("Enter Last Name");
        return false;
    } else if (!/^\S+@\S+\.\w+$/.test(document.myForm.id_email.value)) {
        alert("Not a valid e-mail address");
        return false;
    } else if (!/^\d+$/.test(document.myForm.id_phone.value)) {
        alert("Enter valid Number");
        return false;
    }
    return true;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sign-up form which prompts for the first name, last name, username,
I have a very simple contact form with fields like this: <label> First Name<em>*</em>
I have a form with 3 fields (simplyfied example). The first one is a
I have a HTML form with fields such as name, address, notes, etc. I
I have this form: <form action=contact.php method=post id=contactform> <ol> <li> <label for=name>First Name *
I have a form that consists of: 3 text boxes (first two are required
I have a form with multiple fields that I'm validating (some with methods added
I have form, where some fields are looks like rows, so I can add/delete
I have a table body that grabs it's rows (and form fields) via AJAX
I am using Codeigniter framework and I have a form with input fields. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.