top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to validate input text with phone number using JavaScript?

0 votes
845 views

Write validation functions and modify the onSubmit event Handler in the form code to validate the following form fields:

  1. Phone Number
    • Must be entered
    • Must be below format
    XXX-XXX-XXXX
    XXX.XXX.XXXX
    XXX XXX XXXX
posted May 8, 2017 by Tanay Hanuman Toor

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Here is the code to explain you how to validate phone numbers by javascript :

<!DOCTYPE html>
<html>
<body>

Phone number :






function validatephoneno()
{
var inputtxt = x = document.getElementById("pno").value;
var phoneno = /^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if((inputtxt.match(phoneno))
{
alert("Your phone number is valid");
return true;
}
else
{
alert("Sorry phone number is not valid");
return false;
}
}


That's all. Hope it will help you.

answer Feb 24, 2018 by Vishi Gulati
Similar Questions
+3 votes

My form is somewhat like this:-

 <form id="form1" method="post" name="form1">
 //...fields like firstname, email......
 <button type="submit" id="submit" name="submit" onclick="validate(this)">SUBMIT</button>
 < /form>

The javascript validate function:-

function validate(ref)
{
    if(document.form1.firstname.value=="")
    {
            window.alert("First Name Cannot be Blank");
            return;
    }

    // validating other fields like email... If any field is invalid return 
    document.form1.action="response"
    document.form1.submit()
} 

Now what the problem is suppose I left the firstname field blank and click submit button then windows alerts that firstname field is mandatory....and as soon as I click the OK button in alert form the form is posted...

Now question is that I assign the action attribute after validating all the fields and if any one is invalid the function returns in which case action attribute of the form is not assigned then how the form is submitted.
The form should have been submitted after all the validation tests will have been successful and controls would have reached document.form1.submit(). And what is the solution for this.

I am using Python Flask and rendering template using jinja2.

+2 votes

I have a .js file in which i have defined the function as shown.

function resetall(){    
  //document.write("Hello") ------------This one is working
  var elements = document.getElementsByTagName("input");
  for (var i=0; i < elements.length; i++) {
   if (elements[i].type == "text") {
     elements[i].value = "";
   }
  }
}

I have a resetall button in .html file

<button type="button" value="Reset" onclick="javascript:resetall();">RESET</button>

I tested the reset button by putting a document.write("Hello") to ensure that function is being invoked or not, and it is all right, But text fields are not being cleared. I am not able to figure out what is the problem. Even I have tested document.getelementbyId().

...