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
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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.