Nymo
August 1, 2010, 9:23am
1
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;
}
Nymo
August 2, 2010, 7:39am
3
Thank you. The script work good.
Nymo
August 1, 2010, 2:09pm
4
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.
Nymo
August 1, 2010, 2:39pm
6
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('Пожалуйста, введите <?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;
}
Nymo
August 1, 2010, 1:29pm
9
Thank you. I wrote “ggggg” in this field but the script didn’t noticed this.
Nymo
August 2, 2010, 7:21am
10
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;
}
}