Validating Phone Numbers Using Javascript

前端 未结 9 2142
刺人心
刺人心 2021-01-06 00:41

I\'m working on a web form with several fields and a submit button. When the button is clicked, I have to verify that the required text boxes have been filled in and that th

相关标签:
9条回答
  • 2021-01-06 01:07
    if (charCode > 47 && charCode < 58) {
        document.getElementById("error").innerHTML = "*Please Enter Your Name Only";
        document.getElementById("fullname").focus();
        document.getElementById("fullname").style.borderColor = 'red';
        return false;
    } else {
        document.getElementById("error").innerHTML = "";
        document.getElementById("fullname").style.borderColor = '';
        return true;
    }
    
    0 讨论(0)
  • 2021-01-06 01:11
    <!DOCTYPE html>
    <html>
        <head>
            <style>
               .container__1{
                   max-width: 450px;
                   font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
               }
               .container__1 label{
                   display: block;
                   margin-bottom: 10px;
               }
               .container__1 label > span{
                   float: left;
                   width: 100px;
                   color: #F072A9;
                   font-weight: bold;
                   font-size: 13px;
                   text-shadow: 1px 1px 1px #fff;
               }
               .container__1 fieldset{
                   border-radius: 10px;
                   -webkit-border-radious:10px;
                   -moz-border-radoius: 10px;
                   margin: 0px 0px 0px 0px;
                   border: 1px solid #FFD2D2;
                   padding: 20px;
                   background:#FFF4F4 ;
                   box-shadow: inset 0px 0px 15px #FFE5E5;
    
    
               }
               .container__1 fieldset legend{
                   color: #FFA0C9;
                   border-top: 1px solid #FFD2D2 ;
                   border-left: 1px solid #FFD2D2 ;
                   border-right: 1px solid #FFD2D2 ;
                   border-radius: 5px 5px 0px 0px;
                   background: #FFF4F4;
                   padding: 0px 8px 3px 8px;
                   box-shadow: -0px -1px 2px #F1F1F1;
                   font-weight: normal;
                   font-size: 12px;
               }
               .container__1 textarea{
                   width: 250px;
                   height: 100px;
               }.container__1 input[type=text],
               .container__1 input[type=email],
               .container__1 select{
                   border-radius: 3px;
                   border: 1px solid #FFC2DC;
                   outline: none;
                   color: #F072A9;
                   padding: 5px 8px 5px 8px;
                   box-shadow: inset 1px 1px 4px #FFD5E7;
                   background: #FFEFF6;
                   
    
               }
               .container__1 input[type=submit],
               .container__1 input[type=button]{
                   background: #EB3B88;
                   border: 1px solid #C94A81;
                   padding: 5px 15px 5px 15px;
                   color: #FFCBE2;
                   box-shadow: inset -1px -1px 3px #FF62A7;
                   border-radius: 3px;
                   font-weight: bold;
               }
               .required{
                   color: red;
               }
            </style>
        </head>
        <body>
            <div class="container__1">
                <form name="RegisterForm" onsubmit="return(SubmitClick())">
                    <fieldset>
                        <legend>Personal</legend>
                        <label for="field1"><span >Name<span class="required">*</span><input id="name" type="text" class="input-field" name="Name" value=""</label>
                        <label for="field2"><span >Email<span class="required">*</span><input placeholder="Ex: csa123@yahoo.in" id="email" type="email" class="input-field" name="Email" value=""</label>
                        <label for="field3"><span >Phone<span class="required">*</span><input placeholder="+919853004369" id="mobile" type="text" class="input-field" name="Mobile" value=""</label>
                        <label for="field4">
                            <span>Subject</span>
                            <select name="subject" id="subject" class="select-field">
                                <option value="none">Choose Your Sub..</option>
                                <option value="Appointment">Appiontment</option>
                                <option value="Interview">Interview</option>
                                <option value="Regarding a post">Regarding a post</option>
                            </select>
                        </label>
                        <label><span></span><input type="submit"  ></label>
                    </fieldset>
                </form>
            </div>
        </body>
        <script>
            function SubmitClick(){
            _name = document.querySelector('#name').value;
            _email = document.querySelector('#email').value;
            _mobile = document.querySelector('#mobile').value;
            _subject = document.querySelector('#subject').value;
              if(_name == '' || _name == null ){
                  alert('Enter Your Name');
                  document.RegisterForm.Name.focus();
                  return false; 
              } 
              var atPos = _email.indexOf('@');
             var dotPos = _email.lastIndexOf('.');
    
              if(_email == '' || atPos<1 || (dotPos - atPos)<2){
                  alert('Provide Your Correct Email address');
                  document.RegisterForm.Email.focus();
                  return false;
              }
              var regExp = /^\+91[0-9]{10}$/;
              if(_mobile == '' || !regExp.test(_mobile)){
                  alert('Please Provide your Mobile number as Ex:- +919853004369');
                  document.RegisterForm.Mobile.focus();
                  return false;
              }
              if(_subject == 'none'){
                  alert('Please choose a subject');
                  document.RegisterForm.subject.focus();
                  return false;
              }else{
                alert (`success!!!:--'\n'Name:${_name},'\n' Mobile: ${_mobile},'\n' Email:${_email},'\n' Subject:${_subject},`)
              }
            
              
            }
        </script>
    </html>
    
    0 讨论(0)
  • 2021-01-06 01:17

    function telephoneCheck(str) {
      var a = /^(1\s|1|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/.test(str);
      alert(a);
    }
    telephoneCheck("(555) 555-5555");

    Where str could be any of these formats: 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555

    0 讨论(0)
提交回复
热议问题