0

Here is my javascript validation code:

function Register() {
    if (document.getElementById("<%=Textusername.ClientID%>").value == "") {
        alert("Enter Your Name !");
        document.getElementById("<%=Textusername.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textpwd.ClientID%>").value == "") {
        alert("Enter Your password !");
        document.getElementById("<%=Textpwd.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textemail.ClientID%>").value == "") {
        alert("Enter Your email!");
        document.getElementById("<%=Textemail.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textphone.ClientID%>").value == "") {
        alert("Enter Your phone num!");
        document.getElementById("<%=Textphone.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textaddress.ClientID%>").value == "") {
        alert("Enter Your address !");
        document.getElementById("<%=Textaddress.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textqualification.ClientID%>").value == "") {
        alert("Enter Your qualification!");
        document.getElementById("<%=Textqualification.ClientID%>").focus();
        return false;
    }
    var emailpat = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    var emailid = document.getElementById("<%=Textemail.ClientID%>").value;
    var matchArray = emailid.match(emailpat);
    if (matcharray == null) {
        alert("Your Email ID seems incorrect. Please try again.");
        document.getElementById("<%=Textemail.ClientID%>").focus();
        return false;
    }
    var phonenumfilter = /^[0-9]+$/;
    var phonenum = document.getElementById("<%=Textphone.ClientID%>").value;
    var matchArray88 = phonenum.match(phonenumfilter);
    if (matchArray88 == null) {
        alert("Your Phone Number seems incorrect. Please try again.");
        document.getElementById("<%=Textphone.ClientID%>").focus();
        return false;
    }
}

I created register form with some validation,

When i enter invalid email or phone number, it doesn't show alert.

But when i leave all the fields are empty, it shows alert.

May i know, the error?

Thanks,

pcs
  • 1,864
  • 4
  • 25
  • 49
  • What did you enter for phone number..?@@" – User2012384 Oct 26 '15 at 06:47
  • where are you calling the phone number validation? You are only checking if it empty or not – brk Oct 26 '15 at 06:52
  • I cant understand.. what you are saying... – pcs Oct 26 '15 at 06:52
  • Actually i m new to .net.. can anyone guide me to fix this issue? Thanks – pcs Oct 26 '15 at 06:54
  • You mean asking about markup code? @user2181397 – pcs Oct 26 '15 at 06:56
  • if (document.getElementById("<%=Textphone.ClientID%>").value == "") { alert("Enter Your phone num!"); document.getElementById("<%=Textphone.ClientID%>").focus(); return false; } .This will only check if this field is empty but you also want to validate a number . So also need to test it with this phone number validator which you wrote at the bottom – brk Oct 26 '15 at 06:57
  • Check this fiddle https://jsfiddle.net/m6aktt8s. Also it is very unlikely that id of an element can be in form of scriplet – brk Oct 26 '15 at 07:02

2 Answers2

0

Check if this helps.

Email validation

function validateEmail(form_id,email) {

   var regEmailExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(!regEmailExp.test(address)) {     
      alert('Invalid Email Address');
      return false;
   }
}

Phone Number

function IsValidPhoneNumber(txtPhNumber) {

var ph = ^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$;

var txtPhNo = document.getElementById(txtPhNumber);

if (ph.test(txtPhNo.value) == false) {

    alert("Please enter valid phone number.");

    txtPhNo.focus();

    return false;

}

return true;

}
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
0

For Phone Number Try this Validation

    <asp:TextBox ID="txtmobileno" onpaste="return false;" runat="server" CssClass="form-control" MaxLength="10" OnClientClick="javascript:return Validate()" ></asp:TextBox>

    function Validate(){
    var phone = document.getElementById("<%=txtemail.ClientID%>");
    var RE = /^[\d\.\-]+$/;
if(phone.value != "")
{
    if(!RE.test(phone.value))
    {
        alert("You have entered an invalid phone number");
        return false;
    }
 }
 else{

      alert("please Enter mobile number");
          return false;
     }
   return true;
}

For Email

<asp:TextBox ID="txtemail" onpaste="return false;" MaxLength="50" runat="server" CssClass="form-control"></asp:TextBox>





  if (document.getElementById("<%=txtemail.ClientID%>").value != "")
         {
            var filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (!filter.test(document.getElementById("<%=txtemail.ClientID%>").value)) 
      {
        alert('Please Provide A Valid Email Id');
       document.getElementById("<%=txtemail.ClientID%>").value="";
       document.getElementById("<%=txtemail.ClientID%>").focus();
       return false;
                            }
                        }
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
  • See.. i feel so sad.. i need script with asp .net .. but you just provide me answer only in javascript... – pcs Oct 26 '15 at 08:17
  • @rani Are you asking for server side ? its asp.net and its a working example. see how i'm declaring textbox id `<%=txtemail.ClientID%>` – Krsna Kishore Oct 26 '15 at 08:43
  • thanks for your answer, for email validation, you could not add for "if email field is empty" right? – pcs Oct 26 '15 at 16:11
  • @rani cssclass is applying css to this control .If email is mandatory you can add that !! I just gave an example how it does and i'm just checking for validation if its valid or not. – Krsna Kishore Oct 26 '15 at 16:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93387/discussion-between-rani-and-webruster). – pcs Oct 26 '15 at 16:14
  • hi.. you have any idea in this link http://stackoverflow.com/questions/33350375/oledbexception-was-unhandled-by-user-code-in-c-sharp/33350816#33350375 – pcs Oct 26 '15 at 16:56