...

View Full Version : Validation problem



buildrebuild
07-17-2007, 12:03 PM
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>

vwphillips
07-17-2007, 12:30 PM
http://www.codingforums.com/showthread.php?t=118774

buildrebuild
07-17-2007, 12:34 PM
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?

abduraooft
07-17-2007, 12:38 PM
try this (refer object's value, not a variable having its value)

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")
}

buildrebuild
07-17-2007, 12:48 PM
Sorted. Thanks abd... and thanks everyone else!

Philip M
07-17-2007, 01:08 PM
http://www.codingforums.com/showthread.php?t=118774

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.

buildrebuild
07-17-2007, 08:44 PM
Thakns Philip. Much appreciated!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum