I’m trying to write a function that validates an IPv4 address.
Everything appears ok but it doesn’t appear to pick up the correct class.
My code is here: http://jsfiddle.net/felix001/X7EuJ/23/
<label for="input">IP</label>
<input type="text" name="input" id="input" size="22" />
<input type="submit" value="submit" "whatmask_input" id="submit" align="right" />
JavaScript:
function ipv4addr(x) {
var REGEX = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if ($x != $REGEX) {
$("#input").addClass(".bad");
}
else {
$("#input").addClass(".good");
}
}
$(document).ready(function() {
$("#submit").click(ipv4addr("#input"));
});
Could anyone tell me the best way to troubleshoot this issue?
Thanks,
Load your browser’s development tools console (eg, in Chrome) while you have the fiddler page open.
The first error is:
There is no need to prefix variable names with a $,
$fooandfooare two different variable names. If you havevar foo, thenfoois the variable name.And looking at the code, this probably doesn’t do what you expect:
That would be the same as:
That is, pass the return value of calling
ipv4addr(which is always undefined) into the click method. You probably want to pass a function to get called on every click instead.