Jump to content

Recommended Posts

I have the following code which checks a disclaimer box is ticked and also checks a field (productuse) has some text in. I also have a field called custemail which I want to validate is an email address otherwise return false. Could somebody please help me add the appropriate 'else if' statement to do this, I've tried several scripts out there but just can't get them to work with this current script:

 

<script LANGUAGE="JavaScript">
<!--
function validate(frm) {
    //
    // Check the Email field to see if any characters were entered
    //
    if (frm.productuse.value.length == 0)
    {
        alert("Please tell us why you require this product.")
        frm.productuse.focus()
        return false
    }
    else if (frm.disclaimer.checked == false)
    {
        alert("You must read the disclaimer before requesting a quote.")
        frm.disclaimer.focus()
        return false
    }
}
//-->
</SCRIPT>

 

Sorry, I'm a complete javascript n00b!  :o

Link to comment
https://forums.phpfreaks.com/topic/148589-please-help-with-this/
Share on other sites

There's no existing function in JavaScript to validate an email (at least to my knowledge) so you'd probably need to use a custom function (there's loads on Google - I'll just use the one from first result) ..

 

<script LANGUAGE="JavaScript">
<!--
function validate(frm) {
    //
    // Check the Email field to see if any characters were entered
    //
    if (frm.productuse.value.length == 0)
    {
        alert("Please tell us why you require this product.");
        frm.productuse.focus();
        return false;
    }
    else if (frm.disclaimer.checked == false)
    {
        alert("You must read the disclaimer before requesting a quote.");
        frm.disclaimer.focus();
        return false;
    }
    else if (is_email(frm.custemail.value) == false)
    {
        alert("Please enter a valid email address.");
        frm.custemail.focus();
        return false;
    }
}

function is_email(str) {
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);

    if (str.indexOf(at)==-1){
        return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }

    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }

    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }

    if (str.indexOf(" ")!=-1){
        return false;
    }

    return true;				
}
//-->
</SCRIPT>

 

Not tested but should work no probs!

 

Adam

 

 

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.