• Allow user to enter only alphabets



    <input type="text" class="form-control nameValidation" placeholder="Name " id="txtName" name="txtName" />


    <script>

        $(document).ready(function () {

    //validation for Name field

    $(".nameValidation").keypress(function(event){

    var inputValue = event.which;

    // allow letters and whitespaces only.

    if(!(inputValue >= 65 && inputValue <= 122) && (inputValue != 32 && inputValue != 0)) { 

    swal("Warning", "Please enter only alphabet characters", "warning");

    }

    });

    //can paste only alphabet 

     

    var alphaOnlyInput = document.getElementsByClassName('nameValidation');

    var numofnameValidation = alphaOnlyInput.length;

    alphaOnlyPattern = new RegExp('^[a-zA-Z ]+$');


    let previousValue = '';

    for (var i = 0; i < numofnameValidation; i++) {

    var data=alphaOnlyInput[i];

    data.addEventListener('input', (e) => {

    let currentValue = data.value;

    if (e.inputType.includes('delete') || alphaOnlyPattern.test(currentValue)) {

    previousValue = currentValue;

    }

    data.value = previousValue;

    });

    }

    //in name input alphabet validation

    });

    </script>


  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari