Jump to content

Form Validation Field Help


Zergman

Recommended Posts

I am using the following script to validate my form.

<!--

function validate_form ( )
{
valid = true;

        if ( document.myform.Agent_TID.value == "" )
        {
                alert ( "Please fill in the 'Agent TID' box." );
                valid = false;
        }

        if ( ( document.myform.radio[0].checked == false ) && ( document.myform.radio[1].checked == false ) )
        {
                alert ( "Please choose a Province: AB or BC" );
                valid = false;
        }

	if ( document.myform.notes.value == "" )
        {
                alert ( "Please add Notes. \n\n STN and Details required if a tracking option is selected!!!" );
                valid = false;
        }

	if ( document.myform.resolutioncomments.value == "" )
        {
                alert ( "Resolution Comments can not be blank." );
                valid = false;
        }

	if ( document.myform.country.selectedIndex == 0 )
	{
			alert ( "1" );
			valid = false;
	}

	if ( document.myform.state.selectedIndex == 0 )
	{
			alert ( "2" );
			valid = false;
	}

	if ( document.myform.city.selectedIndex == 0 )
	{
			alert ( "3" );
			valid = false;
	}


        return valid;
}

//-->

</script>

 

What I need to change is the first one to be 6 numeric chars only and can't figure it out.

document.myform.Agent_TID

 

How would I do this so it only accepts 6 numeric characters only?

Link to comment
https://forums.phpfreaks.com/topic/136763-form-validation-field-help/
Share on other sites

        if ( !document.myform.Agent_TID.value.match(/^\d{6}$/) )
        {
                alert ( "Please fill in the 'Agent TID' box." );
                valid = false;
        }

 

However, I would also suggest you detemine all the errors and then display the list to the user - it's much less obtrusive.

 

Example

function validate_form ( )
{
    errors = new Array();

    if ( !document.myform.Agent_TID.value.match(/^\d{6}$/) )
    {
        errors[errors.length] = " - Please fill in the 'Agent TID' box.";
    }

    if ( ( !document.myform.radio[0].checked ) && ( !document.myform.radio[1].checked ) )
    {
        errors[errors.length] = " - Please choose a Province: AB or BC";
    }

    if ( document.myform.notes.value == "" )
    {
        errors[errors.length] = "Please add Notes:\n    STN and Details required if a tracking option is selected!";
    }

    if ( document.myform.resolutioncomments.value == "" )
    {
        errors[errors.length] = " - Resolution Comments can not be blank.";
    }

    if ( document.myform.country.selectedIndex == 0 )
    {
        errors[errors.length] = " - You must select a country.";
    }

    if ( document.myform.state.selectedIndex == 0 )
    {
        errors[errors.length] = " - You must select a state.";
    }

    if ( document.myform.city.selectedIndex == 0 )
    {
        errors[errors.length] = " - You must select a city.";
    }

    if (!errors.length)
    {
        return true;
    }

    message = 'The following errors occured:\n\n';
    for (i=0; i<errors.length; i++)
    {
        message += String(i+1) + errors[i] + '\n';
    }
    alert(message);
    return false;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.