dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Customized Javascript validation for Wordpress plugin

  1. #1
    Join Date
    Aug 2017
    Posts
    1

    Smile Customized Javascript validation for Wordpress plugin

    Hi everybody,

    by job issues, I´m startint to work with javascript but sometimes, the requirements are more complicated than the level I´ve reach by the moment. Thank you in advance for your help.

    I´ve a js code that works right outside wordpress, that serves to validate an IBAN account.

    Code:
    function fn_ValidateIBAN(iban) {
    
     
    
        //Se pasa a Mayusculas
    
        iban = iban.toUpperCase();
    
        //Se quita los blancos de principio y final.
    
        iban = trim(iban);
    
        iban = iban.replace(/\s/g, ""); //Y se quita los espacios en blanco dentro de la cadena
    
     
    
        var letra1,letra2,num1,num2;
    
        var isbanaux;
    
        var numeroSustitucion;
    
        //La longitud debe ser siempre de 24 caracteres
    
        if (iban.length != 24) {
    
            return false;
    
        }
    
     
    
        // Se coge las primeras dos letras y se pasan a números
    
        letra1 = iban.substring(0, 1);
    
        letra2 = iban.substring(1, 2);
    
        num1 = getnumIBAN(letra1);
    
        num2 = getnumIBAN(letra2);
    
        //Se sustituye las letras por números.
    
        isbanaux = String(num1) + String(num2) + iban.substring(2);
    
        // Se mueve los 6 primeros caracteres al final de la cadena.
    
        isbanaux = isbanaux.substring(6) + isbanaux.substring(0,6);
    
     
    
        //Se calcula el resto, llamando a la función modulo97, definida más abajo
    
        resto = modulo97(isbanaux);
    
        if (resto == 1){
    
            return true;
    
        }else{
    
            return false;
    
        }
    
    }
    
     
    
    function modulo97(IBAN) {
    
        var parts = Math.ceil(IBAN.length/7);
    
        var remainer = "";
    
     
    
        for (var i = 1; i <= parts; i++) {
    
            remainer = String(parseFloat(remainer+IBAN.substr((i-1)*7, 7))%97);
    
        }
    
     
    
        return remainer;
    
    }
    
     
    
    function getnumIBAN(letra) {
    
        ls_letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
        return ls_letras.search(letra) + 10;
    
    }
    On the other hand, I´ve a wordpress plugin that serves to manage mail lists and I can add my own text fields to it (in this case the IBAN), in addition to the tipicals name and email. To validate my own text fied, I´ve to modify an existing javascript function of the plugin, embeding with the previus code. The matter is that I don´t know how to call the variable from the own text field. If I open the font code of the page, the field text name is ""required_file[]"" for all the customized fields.

    Code:
    function validateBFTProUser(frm, requireName) {
    
    	requireName = requireName || false;
    
     
    
    	if(requireName && frm.bftpro_name.value=="") {
    
    		alert(bftpro_i18n.name_required);
    
    		frm.bftpro_name.focus();
    
    		return false;
    
    	}
    
     
    
    	if(frm.email.value=="" || frm.email.value.indexOf("@")<1 || frm.email.value.indexOf(".")<1) {
    
    		alert(bftpro_i18n.email_required);
    
    		frm.email.focus();
    
    		return false;
    
    	}
    
    	/*VALIDAR IBAN
    
    	if (frm.iban.value!=="" || frm.iban.value.fn_ValidateIBAN(frm.elements["required_fields[1]"] : 0)) {
    
    		alert(bftpro_i18n.iban_required);
    
    		frm.iban.focus();
    
    		return false;	}
    
    	else{
    
    		
    
    	}
    
    	*/
    
    	//
    
     
    
    	// check custom fields
    
    	var req_cnt = frm.elements["required_fields[]"].length; // there's always at least 1
    
    	if(req_cnt > 1) {
    
    		for(i = 0; i<req_cnt; i++) {
    
    			var fieldName = frm.elements["required_fields[]"][i].value;
    
     
    
    			if(fieldName !='') {
    
    				var isFilled = false;
    
    				// ignore radios
    
    				if(frm.elements[fieldName].type == 'radio') continue;
    
     
    
    				// checkbox
    
    				if(frm.elements[fieldName].type == 'checkbox' && !frm.elements[fieldName].checked) {
    
    					alert(bftpro_i18n.required_field);
    
    					frm.elements[fieldName].focus();
    
    					return false;
    
    				}
    
     
    
    				//o validar en esta otra parte el iban
    
     
    
    				if(frm.elements[fieldName].value == 'field_2') {
    
    					fn_ValidateIBAN(frm.elements["required_fields[1]"];
    
    				}
    
    				else{
    
    					alert(bftpro_i18n.required_field);
    
    					frm.elements[fieldName].focus();
    
    					return false;
    
    				}
    
     
    
    				// all other fields
    
    				if(frm.elements[fieldName].value=="") {
    
    					alert(bftpro_i18n.required_field);
    
    					frm.elements[fieldName].focus();
    
    					return false;
    
    				}
    
    			}
    
    		}
    
    	}
    
     
    
    	// text captcha?
    
    	if(frm.bftpro_text_captcha_answer && frm.bftpro_text_captcha_answer.value == '') {
    
    		alert(bftpro_i18n.missed_text_captcha);
    
    		frm.bftpro_text_captcha_answer.focus();
    
    		return false;
    
    	}
    
     
    
    	return true;
    
    }
    Thanks for your help :)

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,800
    Use test for HTML5 support, if not available the nadd in a support script for the input fields, use HTML5 inputs as they will degrade to normal text inputs if HTML5 isn't supported (most browsers are HTML5 ready) not all the input types are universally supported, thats down to the vendors to implement.

    Use the attributes like "required" when your input is required, you can also use the pattern attribute to have the input checked against regular a expression.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles