skip to Main Content

I have a validator for phone no with js and it is working fine but eve after getting false as return the php code gets executed. how to stop it from executing.

This is my js function

function phonenumber(inputtxt){
    var phoneno = /^d{10}$/;
    if(inputtxt.value.match(phoneno))
    {return true;}
    else
    {alert("message");
     return false;}
}

This is my html code

<form action="get_quote.php" method="post" name="quote" class="quote">
                <font class="label"><input type="text" class="quote" placeholder="Enter Your Name" autocomplete="off" name="name" required/>
                <input type="text" class="quote" autocomplete="off" placeholder="Enter Your Mobile Number" name="mobile" required/>
                <input type="email" class="quote" autocomplete="off" placeholder="Enter Your Email Id" name="email" required/><br />
                    </center>
                <div>
                <div class="rest">
                    <input type="radio" id="new" name="condition" class="rest" checked>New<br />
                    <input type="radio" id="mod" name="condition" class="rest"/>Modification
                </div>
                <div class="rest">
                    <input type="checkbox" name="web"  />Website<br />
                    <input type="checkbox" name="android" />Android Application<br />
                    <input type="checkbox" name="ios" />IOS Aplication<br />
                    <input type="checkbox" name="seo" />SEO/SMO<br />
                    <input type="checkbox" name="desktop" />Computer Software<br />
                </div>
                </div>
                <br><center>
                <textarea name="desc" placeholder="Enter Your Description" required></textarea><br /></font>
                <button type="submit" onclick="phonenumber(document.quote.mobile)" class="quote">Get Quote</button>
            </form>

2

Answers


  1. Working Fiddle.

    Add return in onclick before function call :

    <button type="submit" onclick="return phonenumber(document.quote.mobile)" class="quote">Get Quote</button>
    

    Hope this helps.

    Login or Signup to reply.
  2. You can get mobile field value by using this:

    function phonenumber()
    { 
        var inputtxt = document.getElementsbyName("mobile");
        var phoneno = /^d{10}$/;                 
    
        if(inputtxt.value.match(phoneno)) 
        {
    
            return true;
        } 
        else 
        {
            alert("message"); 
            return false;
        } 
    } 
    

    And than no need to pass param in onclick event, use this:

    <button type="submit" onclick="phonenumber()" class="quote">Get Quote</button>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search