Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validation problem

    Hi
    I have this and it works apart from the phone number validation. I need it to prepend numbers into the input box if there are only 7 digits.
    It recognises that the input box has 7 digits ( because it fires up the alert box) but it won't prepend the "028" before it. How do you do this, its driving me nuts?!


    <html>
    <head>
    <script type="text/javascript">

    function createRequestObject() {
    var ro
    var browser = navigator.appName
    if(browser == "Microsoft Internet Explorer"){
    ro = new ActiveXObject("Microsoft.XMLHTTP")
    }else{
    ro = new XMLHttpRequest()
    }
    return ro
    }

    var http = createRequestObject()

    function sndReq(action) {

    http.open('get', 'cgi/ajaxreadwrite.pl?firstname='+action, true)
    http.onreadystatechange = handleResponse
    http.send(null)
    }

    function sndWrite() {

    var entryName=document.getElementById("entryName").value
    var entryLastName=document.getElementById("entryLastName").value
    var entryAddress=document.getElementById("entryAddress").value
    var entryPhone=document.getElementById("entryPhone").value
    var entryEmail=document.getElementById("entryEmail").value

    if (entryName == "" || entryName.match(/[^a-zA-Z]/)){
    alert("Please enter a First Name. Be sure that the name contains only letters.")
    }

    else if (entryLastName == "" || entryLastName.match(/[^a-zA-Z]/)){
    alert("Please enter a Last Name. Be sure that the name contains only letters.")
    }

    else if (entryEmail.indexOf("@") == -1 || entryEmail.indexOf(".") == -1 || entryEmail.indexOf("@") == 0) {
    alert("Please be sure the email address uses the format name@domain.com")
    }

    if(!entryPhone.match(/^[0-9]+$/)){
    alert("Please make sure your phone number contains just numbers")
    }

    if (entryPhone.length == 7) {
    entryPhone= "028"+entryPhone;
    alert("wrong")
    }


    else{

    entryName=escape(entryName)
    entryLastName=escape(entryLastName)
    entryAddress=escape(entryAddress)
    entryPhone=escape(entryPhone)
    entryEmail=escape(entryEmail)

    var firstString='entryName='+entryName
    var secondString='entryLastName='+entryLastName
    var addressString='entryAddress='+entryAddress
    var phoneString='entryPhone='+entryPhone
    var emailString='entryEmail='+entryEmail

    var sendString=firstString+'&'+secondString+'&'+addressString+'&'+phoneString+'&'+emailString

    http.open('post', 'cgi/ajaxphonewrite.pl');
    http.send(sendString);
    }
    }


    function handleResponse() {

    if(http.readyState == 4){

    var response = http.responseText;
    var update = new Array()

    if(response.indexOf('|')) {
    update = response.split('|')

    theName.innerHTML = document.getElementById('first').value
    address.innerHTML = update[0]
    phone.innerHTML = update[1]
    email.innerHTML = update[2]

    }

    }

    }

    </script>
    </head>

    <body>
    <form id=addEntry">
    <table>
    <tr>
    <td align="right">First Name:</td>
    <td><input type="text" id="entryName"></td>
    </tr>
    <tr>
    <td align="right">Last Name:</td>
    <td><input type="text" id="entryLastName"></td>
    </tr>
    <tr>
    <td align="right">Address:</td>
    <td><input type="text" id="entryAddress"></td>
    </tr>
    <tr>
    <td align="right">Phone:</td>
    <td><input type="text" id="entryPhone"></td>
    </tr>
    <tr>
    <td align="right">Email:</td>
    <td><input type="text" id="entryEmail"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="button" value="Send Entry" onClick="sndWrite()"></td>
    </tr>
    </table>
    </form>

    <BR/><BR/><BR/><BR/>

    <form id="search">
    <table>
    <tr>
    <td align="right"><input type="text" id="first"></td>
    <td><input type="button" value="Search Phonebook"
    onClick="sndReq(document.getElementById('first').value)"></td>
    </tr>
    </table>
    </form>

    <div id="theName"></div>
    <div id="address"></div>
    <div id="phone"></div>
    <div id="email"></div>

    </body>

    </html>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah.
    I've seen that and tried it, but it doesn't work. If you copy my whole script and try adding that code, it doesn't prepend. What am I doing wrong?

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,855
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    try this (refer object's value, not a variable having its value)
    Code:
    var entryPhone=document.getElementById("entryPhone")
    var entryEmail=document.getElementById("entryEmail").value
    
    if(!entryPhone.value.match(/^[0-9]+$/)){
    alert("Please make sure your phone number contains just numbers")
    }
    
    if (entryPhone.value.length == 7) {
    entryPhone.value= "028"+entryPhone.value;
    
    alert("wrong")
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorted. Thanks abd... and thanks everyone else!

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by vwphillips View Post
    As I see it you were given the solution here, and appeared satisfied.

    if(!entryPhone.value.match(/^[0-9]+$/)){
    alert ("Please make sure your phone number contains just numbers");
    entryPhone.value = "";
    entryPhone.focus();
    return false;

    }

    I see nothing in your current script about 10-digit phone numbers.

    BTW, proper names may contain characters other than letters - e.g. O'Hara, Smith-Jones.

  • #7
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thakns Philip. Much appreciated!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •