Saturday, 15 January 2011

validation - How to validate input fields ensuring specific string patterns in Javascript -



validation - How to validate input fields ensuring specific string patterns in Javascript -

trying validate if user has entered name starting letter, @ to the lowest degree 8 characters long, , has @ to the lowest degree 1 number in it. see code below:-

the first 2 conditions have been able create work, validating whether or not there's number within. have tried run function number validation in cant seem work. latest effort create work, help appreciated, maintain in mind first year pupil :)

function nameverify() { var char1; var char2; var index; var nl = "\n"; var valid = false; char1 = usenam.substr(0, 1); char1 = char1.touppercase(); char2 = usenam.substr(1); (index = 1; index <=usenam.length; index++){ while (!valid) { if ((char1 <"a" || char1 >"z") || (usenam.length <8) && (char2 >=0 || char2 <=9)){ alert("alert 1"); usenam = prompt("prompt 2"); char1 = usenam.substr(0, 1); char1 = char1.touppercase(); char2 = usenam.substr(1); } else { valid = true; alert("congragulations, entered correctly"); } } }}

var usenam; usenam = prompt("prompt 1"); result = nameverify(usenam);

/** * @param {string} str name test * @return {boolean} true if str valid */ function isvalidname(str) { homecoming /^[a-za-z][a-za-z0-9]{7,}$/.test(str) && /\d/.test(str) }

/^[a-za-z][a-za-z0-9]{7,}$/ tests starts letter, @ to the lowest degree 8 characters long, , characters letters or numbers. /\d/ tests contains @ to the lowest degree 1 number. see mdn's regexp documentation reference in particular special characters , x{n,} syntax described there. if allow underscores utilize /^[a-za-z]\w{7,}$/ first test.

javascript validation

No comments:

Post a Comment