Phone field validation

Hello,

I don’t know the javascript well.
I have forum validation script but this script can’t validate the phone number field. My script:

if(document.getElementById(‘user_phone’).value==‘’)
{
alert(Please, enter <?php _e(PHONE_TEXT) ?>');
document.getElementById(‘user_phone’).focus();
return false;
}

This script need to recompose that it validates entering data. I found validation script and tried to recompose this script but didn’t success. Who can help me?

function checkPhone (strng) {
var error = “”;
if (strng == “”) {
error = "You didn’t enter a phone number.
";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ‘’); //strip out acceptable non-numeric characters
if (isNaN(parseInt(stripped))) {
error = “The phone number contains illegal characters.”;

}
if (!(stripped.length == 10)) {
error = "The phone number is the wrong length. Make sure you included an area code.\

";
}
return error;
}

Replace the existing phone validation with:

var phone = document.getElementById('user_phone').value;
phone = phone.replace(/[ \\D]+/g,'');
if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
    alert('incorrect phone number');
    document.getElementById('user_phone').focus();
    return false;
    }
else {
    document.getElementById('user_phone').value = phone;
    }

:slight_smile: Thank you. The script work good.

My form validation script start like this function “function chk_form_reg()”. After this goes:

{
if(document.getElementById(‘user_login1reg’).value == ‘’)
{
alert(“Please, enter <?php _e(USERNAME_TEXT) ?>”);
document.getElementById(‘user_login1reg’).focus();
return false;
}
I wrote your script below:

function validate() {
var phone = document.getElementById(‘user_phone’).value;
phone = phone.replace(/[ \D]+/g,‘’);
if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
alert(‘incorrect phone number’);
phone.focus();
return false;
}
else {
document.getElementById(‘user_phone’).value = phone;
}
}

How have you implemented the function in your document? Anything except a valid number will get caught.

Dear friend,please,look at attached file.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>

<script type="text/javascript">
window.onload=function() {
var aObj=document.getElementsByTagName('form');
aObj[0].onsubmit=function() {return validate(this);};
};

function validate() {
if(document.getElementById('user_login1reg').value == '')
{
alert("Please, enter <?php _e(USERNAME_TEXT) ?>");
document.getElementById('user_login1reg').focus();
return false;
}

var phone = document.getElementById('user_phone').value;
phone = phone.replace(/[ \\D]+/g,'');
if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
    alert('incorrect phone number');
    document.getElementById('user_phone').focus();
    return false;
    }
else {
    document.getElementById('user_phone').value = phone;
    }
}
</script>

<style type="text/css">
* {margin:0;padding:0;}
</style>

</head>
<body>
<form action="#" method="" name="form1">
<div>
<label>login: <input type="text" id="user_login1reg" name="user_login1reg"></label>
<label>phone: <input type="text" id="user_phone" name="user_phone"></label>
<button type="submit">submit</button>
</div>
</form>
</body>
</html>

Replace your existing phone validation with the code given. Do not create another function.
Replase this:

		if(document.getElementById('user_phone').value=='')
		{
			alert('&#208;&#376;&#208;&#190;&#208;&#182;&#208;&#176;&#208;&#187;&#209;&#402;&#208;&#185;&#209;&#129;&#209;&#8218;&#208;&#176;, &#208;&#178;&#208;&#178;&#208;&#181;&#208;&#180;&#208;&#184;&#209;&#8218;&#208;&#181; <?php _e(PHONE_TEXT) ?>');
			document.getElementById('user_phone').focus();
			return false;
		}

with this:

var phone = document.getElementById('user_phone').value;
phone = phone.replace(/[ \\D]+/g,'');
if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
    alert('incorrect phone number');
    document.getElementById('user_phone').focus();
    return false;
    }
else {
    document.getElementById('user_phone').value = phone;
    }

Thank you. I wrote “ggggg” in this field but the script didn’t noticed this.

Thank you of your help. I did such as you advice but the script didn’t work. It isn’t notice that field empty or entered bad data.
The script:


function validate() {

		if(document.getElementById('user_phone').value=='')
		{
			alert('Please, enter <?php _e(PHONE_TEXT) ?>');
			document.getElementById('user_phone').focus();
			return false;
		}
	var phone = document.getElementById('user_phone').value;
phone = phone.replace(/[ \\D]+/g,'');
if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
    alert('incorrect phone number');
    document.getElementById('user_phone').focus();
    return false;
    }
else {
    document.getElementById('user_phone').value = phone;
    }
		}

Does this help:

function validate() {
var phone = document.getElementById('user_phone').value;
phone = phone.replace(/[ \\D]+/g,'');
if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
    alert('incorrect phone number');
    phone.focus();
    return false;
    }
else {
    document.getElementById('user_phone').value = phone;
    }
}