html - Javascript form validation. Radio buttons and checkboxes. How to validate? -


i'm having issues javascript form validation radio , checkboxes.

i have external .js script function rules linked in html head section.

my rules example:

    function isvalid4digitzip( str ) {     // return if invalid value passed in     if (str+"" == "undefined" || str+"" == "null" || str+"" == "")           return false;      var isvalid = true;      str += "";      // rules: zipstr must 5 characters long, , can contain numbers    // 0 through 9    if (isblank(str) || (str.length != 4) || !isint(str, false))         isvalid = false;     return isvalid; } // end isvalid4digitzip 

and call function section this:

    if (isvalid4digitzip(document.orderbooks.queryselectorall("[name=postcode]")[0].value)) {    } else {     alert("invalid postcode: entered 3 or less digits. please enter 4 digit postcode");     return false;   } 

can 1 write function rules radio , checkboxes using examples.

i can use javascript. no jquery.

thank you.

the first condition checks if str has truthy value, second checks length, , last checks if characters numbers.

p.s. i'm assuming 5 in rules typo , meant 4.

function isvalid4digitzip(str) {  	return str && str.length === 4 && /^\d+$/.test(str);  }    console.log(isvalid4digitzip('1234'));  console.log(isvalid4digitzip('124a'));  console.log(isvalid4digitzip('0000'));  console.log(isvalid4digitzip('12345'));  console.log(isvalid4digitzip('abcd'));


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -