Forum Moderators: open

Message Too Old, No Replies

Help me to change script

Help me to change script that it checks the validation field

         

Nymo

10:00 am on May 31, 2010 (gmt 0)

10+ Year Member



Please, help me to change script that it checks the validation field. The user has to write only numbers in these fields: user_phone, bill_zip and valid email address in the user_email field.
Thanks.


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

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


if(document.getElementById('user_fname').value == '')
{
alert("Please, enter <?php _e(FIRST_NAME_TEXT) ?>");
document.getElementById('user_fname').focus();
return false;
}
<?php
if($mandotary_info['bill_address1'])
{
?>
if(document.getElementById('user_add1').value=='')
{
alert(Please, enter <?php _e(ADDRESS1_TEXT) ?>');
document.getElementById('user_add1').focus();
return false;
}
<?php
}
?>
<?php
if($mandotary_info['bill_address2'])
{
?>
if(document.getElementById('user_add2').value=='')
{
alert(Please, enter <?php _e(ADDRESS2_TEXT) ?>');
document.getElementById('user_add2').focus();
return false;
}
<?php
}
?>



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


<?php
if($mandotary_info['bill_city'])
{
?>
if(document.getElementById('user_city').value=='')
{
alert(Please, enter <?php _e(CITY_TEXT) ?>');
document.getElementById('user_city').focus();
return false;
}
<?php
}
?>
<?php
if($mandotary_info['bill_state'])
{
?>
if(document.getElementById('user_state').value=='')
{
alert(Please, enter <?php _e(STATE_TEXT) ?>');
document.getElementById('user_state').focus();
return false;
}
<?php
}
?>
<?php
if($mandotary_info['bill_country'])
{
?>
if(document.getElementById('user_country').value=='')
{
alert(Please, enter <?php _e(COUNTRY_TEXT) ?>');
document.getElementById('user_country').focus();
return false;
}
<?php
}
?>
<?php
if($mandotary_info['bill_zip'])
{
?>
if(document.getElementById('user_postalcode').value=='')
{
alert(Please, enter <?php _e(POSTAL_CODE_TEXT) ?>');
document.getElementById('user_postalcode').focus();
return false;
}
<?php
}
?>
document.registerform.submit();
}

MichaelBluejay

1:21 pm on Jun 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you get help here, it's best to post only the bare minimum code to reproduce your problem. Nobody wants to wade through pages and pages of code, and when they have to, you'll get less help.

Anyway, what is it *exactly* that you want the script to do, that it doesn't do now? You say, "help me to change script that it checks the validation field", but there is no field called "validation". (Maybe if i scrutinized the pages of code I'd figure it out, but I'd rather not.) I don't know what you're trying to do.

subexpression

4:19 am on Jun 2, 2010 (gmt 0)

10+ Year Member



Nymo,

I wrote a few functions...they might be what you're looking for:

function isNumeric(val){
return !isNaN(parseFloat(val));
}

function isEmail(val){
var rx = /^[^@ ]+@[^@ ]+\.[^@ \.]+$/;
return rx.test(val);
}

/* NUMERIC */
var upc = document.getElementById('user_postalcode');
if(!isNumeric(upc.value)){
alert(Please, enter <?php _e(POSTAL_CODE_TEXT) ?>');
upc.focus();
return false;
}

/* EMAIL */
var email = document.getElementById('user_email');
if(!isEmail(email.value)){
alert("Please, enter <?php _e(EMAIL_TEXT) ?>");
email.focus();
return false;
}

One problem I can see with your approach is that a user might have 2 field validation errors...and there's only one text field focus().
Plus, "return false" will end the function before any other validation can occur.
There might still be more field errors...and it quits...returns false...and forces to user to correct only one field at a time.

subexpression

5:01 am on Jun 2, 2010 (gmt 0)

10+ Year Member



Also, for phone, you would want to validate for specific patterns, not just numeric:
function isPhone(val){ 
var rx = /^1?\-?\(?[1-9]\d{2}\)?\s?\-?\s?\d{3}\s?\-\s?\d{4}|\d{10}$/;
return rx.test(val);
}

/* PHONE */
var ph = document.getElementById('user_phone');
if(!isPhone(ph.value)){
alert(Please, enter <?php _e(PHONE_TEXT) ?>');
ph.focus();
return false;
}

This regular expression takes into account a user may type in his phone number in several acceptable formats:

555-555-5555
555 - 555 - 5555
(555) 555-5555
(555)-555-5555
(555) - 555 - 5555
5555555555
1-555-555-5555
1 (555) 555-5555
...and a few other variations.

You can modify it as you like.

subexpression

5:21 am on Jun 2, 2010 (gmt 0)

10+ Year Member



Nymo,

MichaelBluejay is right...it's best to be as succinct as possible.
You'll get help much faster.

Additionally, this forum's moderators have already mentioned that "code dumps" (very large portions of user code) are discouraged.

"1. Post only relevant code"

Read up on the forum polices:
Javascript Forum Policies for Links and Code [webmasterworld.com]

subexpression

5:39 am on Jun 2, 2010 (gmt 0)

10+ Year Member



Not to be overbearing, but thinking about phone numbers, some people use an extension:
555-555-5555 x12345
555-555-5555 ext1

You can modify the regular expression to include extensions:
var rx = /^1?\-?\(?[1-9]\d{2}\)?\s?\-?\s?\d{3}\s?\-\s?\d{4}(?:\\s*?(x|ext)\\d{1,5})?|\d{10}$/;