Help

Website URL
(http://cdlh2f.kesug.com/hw3)


I am trying to do the first paragraph but am unsure where I should be doing this and what I need to add to get this to work.

This is the code I used to make it work on my side; make sure you have the attributes that call the function set on your input box and also make sure to adapt the script and everything else I used to your own use case and to the guide your teacher told you to use:

<input type="text" id="idthing" name="format" onblur="checkcomplex(this, document.getElementById('formaterror'));" onkeyup="checkcomplex(this, document.getElementById('formaterror'));">
<span id="formaterror"></span>
<script>
	function checkcomplex(whichcontrol,errormsgfield){
		var passcheck=true;
		var x=0;
		if (whichcontrol.value.length==0){
			errormsgfield.innerHTML="";
			return true;
		}
		else
		{
			var regex=/[0-9]{3}-[0-9]{2}-[0-9]{4}/;
			passcheck=regex.test(whichcontrol.value);
		} //end else
		if (passcheck){
			errormsgfield.innerHTML="";
		}
		else{
			errormsgfield.innerHTML="Must be of the form 999-99-9999!";
		}
		return passcheck;
	}
</script>

In short: you should put the onblur and onkeyup attributes on the input box that call the function with as first parameter the input box object itself, this is why I put this on my code, and as second parameter the span where the format error will show up selected by ID, then put the format error span below it (then correct its position with some CSS on a div) and then the script, which should be loaded after the elements or after the page finished loading so to not give an error in the end.

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.